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

3👍

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 makemigrations and python manage.py migrate. You should be fine.

👤rmad17

13👍

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 yourCharFields and TextFields.

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

Leave a comment