模型的字段在管理面板中被禁用

时间:2019-10-27 09:51:11

标签: django python-3.x django-models django-templates

我有一个模型,该模型的名称为article,每篇文章都有一个标题。 我想通过admin创建一个新文章,但是我遇到了只是标题字段突然被禁用并且无法输入标题的问题。我的文章模型如下:

class Article(models.Model):
    title=models.CharField(max_length=100)
    body=models.TextField()
    view=models.IntegerField(default=0)
    created_at=models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now = True)
    published_at = models.DateTimeField(default=timezone.now)
    show=models.BooleanField( default=1)
    def __str__(self):
        return self.title


    user = models.ForeignKey(User,on_delete=models.SET_NULL, null = True)

    categories = models.ManyToManyField(Category)

    class Meta:
        permissions=(
            ('private_section_article','Private Section Article'),
        )

与该模型对应的管理文件为:

@admin.register(Article)
class ArticleAdmin(ModelAdminJalaliMixin,admin.ModelAdmin):
    def published_fa(self,model):
        return datetime2jalali(model.published_at).strftime('%y/%m/%d _ %H:%M:%S')
    list_display=('title','view','published_fa','created_at','updated_at','show')
    list_display_links=('published_fa',)
    search_fields = ['title','body','created_at']
    list_filter=('published_at','title')
    date_hierarchy='updated_at'
    ordering=['-created_at']
    readonly_fields = ('title',)
    actions=['make_hide','make_show']
    fieldsets = (
        (None, {
            "fields": (
                'title','categories','body','published_at'
            ),
        }),
        ('Advanced Options',{
            'classes':('wide', 'extrapretty','collapse'),
            'fields':('view','show','user')
        })
    )

    def make_hide(self,request,queryset):
        row_updated = queryset.update(show=0)
        message='1 article was'
        if row_updated is not 1:
            message="%s articles were" % row_updated

        self.message_user(request,"%s marked as hide" % message)# to show a message after this action have done

    make_hide.short_description='make selected articles as hide'

    def make_show(self , request , queryset):
        row_updated = queryset.update(show = 1)
        message='1 article was'
        if row_updated is not 1:
            message="%s articles were" % row_updated
        self.message_user(request,"%s marked as show" % message)# to show a message after this action have done

    make_show.short_description = "Make selected articles as show"

现在,当我选择之前创建的文章时,管理面板不会为我提供更改它的输入,并且当我要创建一个新文章时,我不能只为title字段输入任何内容,因为没有输入或textFiled之类的东西。 帮助我知道为什么会这样。

  

这是管理面板中的屏幕截图,仅禁用标题字段   the screenshot of admin page to edit a title

1 个答案:

答案 0 :(得分:0)

因为您的{p {1}}

readonly_fields = ('title',)

如果删除该问题将得到解决