Django新手在这里。我有两个这样的简单模型:
class Fluff(models.Model):
# ....
# some fields definitions
class Something(models.Model):
name = models.CharField(max_length=200)
fluff = models.ForeignKey(Fluff)
syncdb
按预期进行,然后我转到shell进行测试:
>>> s = Something()
>>> s.name = 'Blah'
>>> s.fluff = Fluff(...)
>>> s.save()
Traceback (most recent call last): # traceback skipped for brevity
IntegrityError: app_something.fluff_id may not be NULL
我希望Django能够发挥它的魔力,弄清楚fluff
没有保存,然后在保存s
之前保存它。这没有发生,但没关系,我可以为自己保存fluff
,让我们再试一次:
>>> s = Something()
>>> s.name = 'Blah'
>>> s.fluff = Fluff(...)
>>> s.fluff.save()
>>> s.save()
Traceback (most recent call last): # traceback skipped for brevity
IntegrityError: app_something.fluff_id may not be NULL
没有运气。这应该是微不足道的,但我现在无法弄明白。任何帮助将不胜感激。
答案 0 :(得分:2)
尝试做这样的事情:
>>> s.name = 'Blah'
>>> obj = Fluff(...)
>>> obj.save()
>>> s.fluff = obj
>>> s.save()