Django admin gives a warning "Field" X "has no default value"

I created two models from an existing legacy DB, one for articles and one for tags that can be associated with articles:

class Article(models.Model): article_id = models.AutoField(primary_key=True) text = models.CharField(max_length=400) class Meta: db_table = u'articles' class Tag(models.Model): tag_id = models.AutoField(primary_key=True) tag = models.CharField(max_length=20) article=models.ForeignKey(Article) class Meta: db_table = u'article_tags' 

I want to enable tagging for an article from the admin interface, so my admin.py file looks like this:

 from models import Article,Tag from django.contrib import admin class TagInline(admin.StackedInline): model = Tag class ArticleAdmin(admin.ModelAdmin): inlines = [TagInline] admin.site.register(Article,ArticleAdmin) 

The interface looks great, but when I try to save, I get: Warning at /admin/webserver/article/382/ Field 'tag_id' doesn't have a default value

+4
source share
2 answers

The problem was that tag_id not set in the DB as an auto-increment field.

+2
source

This can also happen if your database has an unused field that is not nullable.

+3
source

Source: https://habr.com/ru/post/1333978/


All Articles