from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
这样的两张表,ForeignKey是一对多的关系,还是多对一的关系?
p = Poll(question="What's new?", pub_date=timezone.now())
p.save()
这两句表示插入一条Poll的数据。
p.choice_set.create(choice='Not much', votes=0) 为何表示插入一条Choice的数据呢
p.choice_set表示一个集合,然后create是创建一个新的对象,为何这个对象是Choice?
为何是p.choice_set, p是Poll表的数据,为何可以.choice_set, 我认为同理也可以p.votes_set(可是我的猜想是错误的,votes不可以。)
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
这样的两张表,ForeignKey是一对多的关系,还是多对一的关系?
p = Poll(question="What's new?", pub_date=timezone.now())
p.save()
这两句表示插入一条Poll的数据。
p.choice_set.create(choice='Not much', votes=0) 为何表示插入一条Choice的数据呢
p.choice_set表示一个集合,然后create是创建一个新的对象,为何这个对象是Choice?
为何是p.choice_set, p是Poll表的数据,为何可以.choice_set, 我认为同理也可以p.votes_set(可是我的猜想是错误的,votes不可以。)