Django OneToOne reverse relationship does not allow null values

时间:2017-04-24 17:19:59

标签: database python-2.7 one-to-one django-1.8

I have this architecture (very simplified)

from django.db import Models

class MainClass(models.Model):
    a = models.IntegerField()
    b = models.CharField()

class OtherClass(models.Model):
    c = models.IntegerField()
    main = models.OneToOneField(MainClass, primary_key=True)

Which means my MainClass object has an attribute named otherclass, because of the existence of the reverse relationship between these models.

My problem is if I specify valid values for MainClass.a and MainClass.b, but None for MainClass.otherclass. I get the error

ValueError: Cannot assign None: "MainClass.otherclass" does not allow null values.

I understand there cannot be OtherClass without MainClass (it doesn't make sense), but why the opposite situation is also causing an error? Other way: Why cannot be MainClass without OtherClass?

1 个答案:

答案 0 :(得分:0)

虽然限制this is a normal behaviour in Django 1.8

,但看起来像has been removed in Django 1.10

所以,这不是一个错误。