Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django admin gives warning "Field 'X' doesn't have a default value"

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

like image 618
olamundo Avatar asked Jan 02 '11 09:01

olamundo


3 Answers

This can also happen if you have a disused field in your database that doesn't allow NULL.

like image 64
David Herse Avatar answered Sep 19 '22 05:09

David Herse


The problem was that in the DB, tag_id wasn't set as an autoincrement field.

like image 42
olamundo Avatar answered Sep 22 '22 05:09

olamundo


What solved this issue in my case was disabling STRICT_TRANS_TABLES SQL mode which was enabled by default.

like image 29
Shachar Ashkenazi Avatar answered Sep 22 '22 05:09

Shachar Ashkenazi