Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top