django 多对多关系的操作 包含映射表
2014-02-13 11:42
561 查看
图书馆的书,借书的学生为多对多关系
分为三张表,book ,stduent ,book_relation映射表
在自动生成数据库表的时候,会生成book表,student表,book_relation表,映射表名字的生成规则为models中class Book的名字加上manytomany的名字,即book+relation
插入数据
如果需要增加学生信息则student = Student(sname='1')
否则student = Student.objects.get(id=1)
book.relation.add(student)
book.save()
删除数据
student = Student.objects.get(id=1)
book.relation.remove(student)
book.save()
分为三张表,book ,stduent ,book_relation映射表
class Book: name = models.CharField(max_length=100) relation =models.ManyToManyField(student)
class Student(models.Model): sname = models.CharField(max_length=100)
在自动生成数据库表的时候,会生成book表,student表,book_relation表,映射表名字的生成规则为models中class Book的名字加上manytomany的名字,即book+relation
插入数据
如果需要增加学生信息则student = Student(sname='1')
否则student = Student.objects.get(id=1)
book.relation.add(student)
book.save()
删除数据
student = Student.objects.get(id=1)
book.relation.remove(student)
book.save()
class Student(models.Model): name = models.CharField(max_length=5) cources = models.ManyToManyField(Course, through='StudentCourse') class Course(models.Model): name = models.CharField() class StudentCourse(models.Model): student = models.ForeignKey(Student) course = models.ForeignKey(Course) score=models.FloatField()
相关文章推荐
- django错误 -- TypeError at /regist/
- Google,微软,科大讯飞的语音识别引擎对比
- NYOJ 156 Hangover
- NYOJ 156 Hangover
- google mock
- golang注意Channel的阻塞特性
- If You Want Something You’ve Never Had, You’ve Got To Do Something You’ve Never Done
- amcharts categoryAxis
- Max Subarray
- 富士康将用Google机器人来代工iPhone!
- 3.Go by Example: Variables
- 英雄会(csdn pongo)题解之罐子和硬币
- django学习系列之视图篇
- 1.Go by Example: Hello World
- Going Home(MCMF)
- django学习系列之模板系统篇(二)
- 【翻译】go语言中的map实战
- 加载bitmap的缓存方案(google教程)
- django学习系列之模板系统篇(一)
- 安装Google框架服务并突破Google Play下载限制