Django OneToOne reverse relationship does not allow null values

旧城冷巷雨未停 提交于 2019-12-12 01:51:56

问题


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:


Looks like this is a normal behaviour in Django 1.8, although the restriction has been removed in Django 1.10

So, This isn't an error.



来源:https://stackoverflow.com/questions/43594225/django-onetoone-reverse-relationship-does-not-allow-null-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!