Reputation: 24981
I have created two models out of an existing legacy DB , one for articles and one for tags that one can associate 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 adding tags 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 fine, but when I try to save, I get:
Warning at /admin/webserver/article/382/
Field 'tag_id' doesn't have a default value
Upvotes: 6
Views: 9568
Reputation: 372
What solved this issue in my case was disabling STRICT_TRANS_TABLES
SQL mode which was enabled by default.
Upvotes: 1
Reputation: 471
This can also happen if you have a disused field in your database that doesn't allow NULL.
Upvotes: 5
Reputation: 24981
The problem was that in the DB, tag_id
wasn't set as an autoincrement field.
Upvotes: 3