Django ModelAdmin - fieldsets ...字段'date'从表单中丢失 [英] Django ModelAdmin - fieldsets ... field 'date' missing from the form

查看:128
本文介绍了Django ModelAdmin - fieldsets ...字段'date'从表单中丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出了这个问题的时候出了什么问题。我发布了它,以防它可以帮助别人。



错误:'FooAdmin.fieldsets [0] [1] ['fields']'指的是从表单中缺少的字段'date'。



使用以下代码:

 #models.py 
from django.db import models

class Foo(Base):
date = models.DateField(auto_now_add = True)
title = models.CharField(max_length = 255)

#admin.py
from django.contrib import admin

class FooAdmin(BaseAdmin):
list_display =(title,date)
fieldsets =(
(无,{
fields:(date,title)
})


admin.site.register(Foo,FooAdmin)


解决方案

错误是由于日期有 auto_now_add = True (或 auto_now = True )。

由于该值是自动的,因此它不可编辑,因此它不在表单中。要解决这个问题,请在 FooAdmin 中添加:

  readonly_fields =(日期,)


I figured out what the problem was while writing this question. I post it anyway in case it could help someone else.

The error: 'FooAdmin.fieldsets[0][1]['fields']' refers to field 'date' that is missing from the form.

With the following code:

# models.py
from django.db import models

class Foo(Base):
    date = models.DateField(auto_now_add=True)
    title = models.CharField(max_length=255)

# admin.py
from django.contrib import admin

class FooAdmin(BaseAdmin):
    list_display = ("title", "date")
    fieldsets = (
        (None, {
            "fields": ("date", "title")
        }),
    )

admin.site.register(Foo, FooAdmin)

解决方案

The error is due to date having auto_now_add=True (or auto_now=True).
As the value is automatic, it's not editable, so it's not in the form. To solve that, add this in FooAdmin:

readonly_fields = ("date",)

这篇关于Django ModelAdmin - fieldsets ...字段'date'从表单中丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆