【问题标题】:I get "parent_id may not be NULL" when creating my Django model创建 Django 模型时出现“parent_id may not be NULL”
【发布时间】:2010-10-25 04:22:08
【问题描述】:

我正在创建自己的Group 模型;我不是指内置的Group 模型。我希望每个 hroup 成为另一个组的成员(它是父组),但是有一个“顶级”组没有父组。

管理界面不允许我在未输入父级的情况下创建组。我收到错误personnel_group.parent_id may not be NULL。我的Group 模型如下所示:

class Group(models.Model):
    name = models.CharField(max_length=50)
    parent = models.ForeignKey('self', blank=True, null=True)
    order = models.IntegerField()
    icon = models.ImageField(upload_to='groups', blank=True, null=True)
    description = models.TextField(blank=True, null=True)

我怎样才能做到这一点?

谢谢。

【问题讨论】:

  • 所以实际上标题应该是:为什么syncdb失败,当我将blank=True,null=True添加到模型字段时

标签: python database django models


【解决方案1】:

Django 进化可以让你摆脱这种问题,而不会删除你的完整数据库

【讨论】:

    【解决方案2】:

    我在将 blank=True, null=True 添加到 parent 字段定义之前创建了数据库。 syncdb 无法处理这种类型的更改,因此 Django 没有接受我的更改。

    我删除了我的数据库并让syncdb创建另一个,它工作正常。

    【讨论】:

    • 您应该考虑使用像 South 这样的数据库方案迁移工具。
    • 有没有更好的方法来代替删除整个数据库?
    • @niklasfi 如果您使用 South,则无需删除数据库。
    猜你喜欢
    • 2014-01-08
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 2016-10-24
    • 1970-01-01
    相关资源
    最近更新 更多