基础部分已经写完:【原创】Django-ORM基础
以下部分将对表与表之间的关联操作做以介绍
models.py
1 #_*_coding:utf-8_*_ 2 from django.db import models 3 4 # Create your models here. 5 6 class Colors(models.Model): 7 colors=models.CharField(u'颜色',max_length=10) 8 def __unicode__(self): 9 return self.colors 10 11 class Ball(models.Model): 12 color=models.OneToOneField("Colors") #与颜色表为一对一,颜色表为母表 13 description=models.CharField(u'描述',max_length=10) 14 def __unicode__(self): 15 return self.description 16 17 class Clothes(models.Model): 18 color=models.ForeignKey("Colors") #与颜色表为外键,颜色表为母表 19 description=models.CharField(u'描述',max_length=10) 20 def __unicode__(self): 21 return self.description 22 23 class Child(models.Model): 24 name=models.CharField(u'姓名',max_length=10) 25 favor=models.ManyToManyField('Colors') #与颜色表为多对多