django admin 发出警告“Field ‘X’;没有默认值”
我从现有的旧数据库中创建了两个模型,一个用于文章,一个用于可以与文章关联的标签:
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)
界面看起来不错,但是当我尝试保存时,我得到: /admin/webserver/article/382/ 发出警告 字段“tag_id”没有默认值
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果数据库中有一个不允许 NULL 的废弃字段,也可能会发生这种情况。
This can also happen if you have a disused field in your database that doesn't allow NULL.
问题是在数据库中,
tag_id
未设置为自动增量字段。The problem was that in the DB,
tag_id
wasn't set as an autoincrement field.在我的例子中,解决此问题的方法是禁用默认启用的
STRICT_TRANS_TABLES
SQL 模式。What solved this issue in my case was disabling
STRICT_TRANS_TABLES
SQL mode which was enabled by default.