6

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

olamundo
  • 23,991
  • 34
  • 108
  • 149
  • why are you using `AutoFields` for fields `model_id`, they are automatically created and set as primary keys by django. – crodjer Jan 02 '11 at 09:43
  • @dcrodjer - I am using a legacy DB – olamundo Jan 02 '11 at 09:49
  • 1
    Have you made any model/database table changes (after syncing the db) and not synced them? Look [here](http://manydemons.com/blog/2010/10/field-id-doesnt-have-a-default-value-django-python/) – crodjer Jan 02 '11 at 10:18
  • @dcrodjer - so does this means it's a django bug? seems odd – olamundo Jan 02 '11 at 15:42
  • why django bug you say!! I am asking if you reset the tables and syncd databases – crodjer Jan 02 '11 at 15:50
  • @dcrodjer - I have synced the db. But haven't reset the tables; I am not sure I understand why is that a solution - What will reseting my tables, and reinserting all the data accomplish? will the resulting db be different in any way? In any case, I want to understand the cause of the problem and not just try to hack at it... – olamundo Jan 03 '11 at 01:33

3 Answers3

5

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

David Herse
  • 471
  • 5
  • 8
3

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

olamundo
  • 23,991
  • 34
  • 108
  • 149
1

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

RamenCoder
  • 358
  • 1
  • 2
  • 16