postgresql 在 django admin 中添加新项目时,列“name”中的空值违反了非空约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44783677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:35:33  来源:igfitidea点击:

Null value in column "name" violates not-null constraint when adding new item in django admin

djangopostgresqldjango-admin

提问by Ruben

I have added a new model to my admin. This is my models.py:

我向管理员添加了一个新模型。这是我的models.py:

class EngineeringToolAttributeType(models.Model):
    name = models.CharField(max_length=50)
    description = models.CharField(max_length=255, blank=True, null=True)
    api_url = models.CharField(max_length=255, blank=True, null=True)
    api_field = models.CharField(max_length=50, blank=True, null=True)
    active = models.BooleanField(default=True)

    def __str__(self):
        return self.name

And the admin.py:

和 admin.py:

from extras.models import EngineeringToolAttributeType
from django.contrib import admin

class EngineeringToolAttributeTypeAdmin(admin.ModelAdmin):
    fields = ['name', 'description', 'api_url', 'api_field', 'active']
    list_display = ('name', 'description', 'api_url', 'api_field', 'active')

admin.site.register(EngineeringToolAttributeType, EngineeringToolAttributeTypeAdmin)

When I try to add (click on add button via the admin), I get this error:

当我尝试添加(通过管理员单击添加按钮)时,出现此错误:

Internal Server Error: /website/admin/extras/engineeringtoolattributetype/add/

IntegrityError at /admin/extras/engineeringtoolattributetype/add/
null value in column "name" violates not-null constraint

This has never happened before. I know name is not allowed to be Null, but I'm still adding a record. How is that possible?

这是以前从未发生过的。我知道 name 是不允许的Null,但我仍在添加一条记录。这怎么可能?

采纳答案by rmad17

This has a short answer here.
If you specify a field as not null field django will ask you to either provide a default value in code itself or it will ask you to provide a default value when you run migrations. A better approach would be to provide some sane default in your models.py itself. After that run python manage.py makemigrationsand python manage.py migrate. You should be fine.

这有一个简单的答案在这里
如果您将字段指定为非空字段,django 将要求您在代码本身中提供默认值,或者在运行迁移时要求您提供默认值。更好的方法是在你的 models.py 本身中提供一些合理的默认值。在那之后运行python manage.py makemigrationspython manage.py migrate。你应该没事。

回答by Sardorbek Imomaliev

This issue is partially result of not using CharFieldand TextFieldproperly. You should almostnever use null=Trueon this fields and their subclasses. https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.Field.null

这个问题的部分原因是没有正确使用CharFieldTextField正确。您几乎不应该null=True在此字段及其子类上使用。 https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.Field.null

Avoid using null on string-based fields such as CharField and TextField. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it's redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL. One exception is when a CharField has both unique=True and blank=True set. In this situation, null=True is required to avoid unique constraint violations when saving multiple objects with blank values.

避免在基于字符串的字段(例如 CharField 和 TextField)上使用 null。如果基于字符串的字段具有 null=True,则意味着它有两个可能的“无数据”值:NULL 和空字符串。在大多数情况下,“无数据”有两个可能的值是多余的;Django 约定是使用空字符串,而不是 NULL。一个例外是当 CharField 同时设置 unique=True 和 blank=True 时。在这种情况下,需要 null=True 以避免在保存具有空白值的多个对象时违反唯一约束。

I would highly suggest removing this param from yourCharFields and TextFields.

我强烈建议从您的CharFields 和TextFields 中删除此参数。

Also just to be sure run ./manage.py makemigrations && ./manage.py migrate.

也只是为了确保运行./manage.py makemigrations && ./manage.py migrate