保存文章时,django不会添加自动增量主键ID

时间:2015-04-10 10:04:56

标签: django django-templates

我有一个安装在django管理站点的文章应用程序,当我完成一篇文章的编辑时,我点击了保存按钮,但是出现了错误页面:

enter image description here

文章/ models.py

# blog category models
class Category(models.Model):
    id = models.IntegerField(primary_key=True,help_text='primary key')
    name = models.CharField(max_length=50,help_text='category name')
    description = models.TextField(default='',help_text='category description')
    createtime = models.DateTimeField(auto_now_add=True)
    modifytime = models.DateTimeField(auto_now=True)

    categories = models.Manager()

    class Meta:
        db_table = 'article_category'
    def __str__(self):
        return self.name

#blog article models
class Article(models.Model):
    STATUS = (
        (0,'on'),
        (1,'off')
    )
    id = models.IntegerField(primary_key=True,help_text='primary key')
    category = models.ForeignKey(Category,help_text='foreigner key reference Category')
    title = models.CharField(max_length=100,help_text='article title')
    content = models.TextField(help_text='article content')
    like = models.IntegerField(default=0,help_text='like numbers')
    secretcode = models.CharField(max_length=512,help_text='who has the code can scan')
    status = models.IntegerField(choices=STATUS,help_text='status of the article')
    createtime = models.DateTimeField(auto_now_add=True,help_text='time that first created')
    modifytime = models.DateTimeField(auto_now=True,help_text='time when modified')

    articles = models.Manager()
    class Meta:
        db_table = 'article'

文章/ widgets.py

from pagedown.widgets import AdminPagedownWidget
from django import forms
from .models import Article

class ArticleModelForm(forms.ModelForm):
    content = forms.CharField(widget=AdminPagedownWidget())
    class Meta:
        model = Article
        fields = ('title','category', 'content', 'secretcode', 'status')

文章/ admin.py

from django.contrib import admin
from .widgets import ArticleModelForm
from .models import Article,ArticleImage,Category

class  MMBArticleAdmin(admin.ModelAdmin):
    form = ArticleModelForm

admin.site.register(Article,MMBArticleAdmin)
admin.site.register(Category)
admin.site.register(ArticleImage)

管理网站中的页面如下所示:

enter image description here

然后我点击保存,错误页面如上所示!为什么会发生这种情况?以及如何修复它?

1 个答案:

答案 0 :(得分:3)

您已使用手动非自动增量ID覆盖默认自动字段。不要那样做。完全删除id字段。