我从现有的遗留数据库中创建了两个模型,一个用于文章,一个用于可以与文章关联的标签:
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'
我想从管理界面为文章添加标签,所以我的admin.py
文件如下所示:
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)
界面看起来不错,但是当我尝试保存时,我得到:
Warning at /admin/webserver/article/382/
Field 'tag_id' doesn't have a default value