【问题标题】:Saving OnetoOneField in Django Admin while null=True gives error在 Django Admin 中保存 OnetoOneField 而 null=True 会出错
【发布时间】:2017-04-10 13:48:07
【问题描述】:

我的模型中有 OnetoOnefield,它设置为 null=True 和 blank=True。一切正常,但是当我尝试从管理站点保存该模型的对象时,我得到 IntegrityError,说该字段不能为空。

我的模型是:

class Appointment(models.Model):
    doctor = models.OneToOneField(Doctor)
    clinic = models.OneToOneField(Clinic, null=True, blank=True)
    hospital = models.OneToOneField(Hospital, null=True, blank=True)

【问题讨论】:

  • 确切的错误消息是什么(带有完整的堆栈跟踪)? Appointment.doctorblank=False, null=FalseIntegrityError 是否与该字段相关?
  • 我必须填写给定约会的“诊所”或“医院”,但每当我将这些字段中的任何一个留空时。我收到错误消息,指出此字段不能为空。虽然它们都被标记为 null=True。

标签: django python-2.7 django-models


【解决方案1】:

看看这个字段。

doctor = models.OneToOneField(Doctor)

class Appointment(models.Model):
    doctor = models.OneToOneField(Doctor)
    clinic = models.OneToOneField(Clinic, null=True, blank=True)
    hospital = models.OneToOneField(Hospital, null=True, blank=True)

【讨论】:

    猜你喜欢
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 2014-06-11
    • 1970-01-01
    • 2020-07-17
    • 2012-12-13
    • 1970-01-01
    相关资源
    最近更新 更多