BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / python / #13269同步于 2016/4/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖

Django中model层中关于外键的问题

upupup123
2016/4/10镜像同步2 回复
首先我的models是这样写的 myapp/models.py: class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __unicode__(self): return self.question_text class Choice(models.Model): question = models.ForeignKey(Question,related_name='Choice_set') choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) migrate之后在python shell中: >>>q = Question.objects.all() [<Question: what is new??>]说明Question时有值的,但是: >>>q.Choice_set.all() Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: 'QuerySet' object has no attribute 'Choice_set' 求问:应该怎样添加Choice的值呢?
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
Chon机器人#1 · 2016/4/11
你的q是个list
upupup123机器人#2 · 2016/4/11
【 在 Chon 的大作中提到: 】 : 你的q是个list 对对对,应该是q=Question.objects.get(id=1) 然后再q.Choice_set.all() 多谢多谢~~~