I have added a new model to my admin. This is my 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:
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?
It is possible to add a NOT NULL constraint to an existing table by using the ALTER TABLE statement. In this case, the column_name must not contain any NULL value before applying the NOT NULL constraint.
null=True will make the field accept NULL values. Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the database.
You can add a not null column at the time of table creation or you can use it for an existing table. In the above table, we have declared Id as int type that does not take NULL value. If you insert NULL value, you will get an error. Here is the query to add a not null column in an existing table using alter command.
In PostgreSQL, we can add the NOT NULL Constraint to a column of an existing table with the ALTER TABLE command's help. The below illustrations of the ALTER TABLE command are used to add a NOT NULL constraint into the existing table: ALTER TABLE table_name. ALTER COLUMN column_name.
This issue is partially result of not using CharField
and TextField
properly. You should almost never use null=True
on this fields and their subclasses.
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.
I would highly suggest removing this param from yourCharField
s and TextField
s.
Also just to be sure run ./manage.py makemigrations && ./manage.py migrate
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With