简体   繁体   English

在 Django 管理表单中显示创建和编辑的字段

[英]Show created and edited fields in Django admin form

I have this model我有这个 model

class Volunteer(models.Model):
STATUSES = (
    ('Active', 'Active'),
    ('Paused', 'Paused'),
    ('Inactive', 'Inactive'),
)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email_address = models.CharField(max_length=100, null=True, blank=True)
picture = models.ImageField(null=True, blank=True)
status = models.CharField(max_length=20, choices=STATUSES, default="Active")
created = models.DateTimeField(auto_now_add=True, editable=False)
edited = models.DateTimeField(auto_now=True, editable=False)

And I register it like this我像这样注册它

class VolunteerAdmin(admin.ModelAdmin):
fields = ('first_name', 'last_name', 'email_address', 'status', 'created', 'edited')
list_display = ('first_name', 'last_name', 'email_address', 'status')
list_editable = ('status',)
list_filter = ('status',)
search_fields = ('first_name', 'last_name')

admin.site.register(Volunteer, VolunteerAdmin)

I get an error because I have manually added the created and edited fields as I want to see them in the view/edit forms.我收到一个错误,因为我手动添加了创建和编辑的字段,因为我想在视图/编辑 forms 中看到它们。 I know that the user should not be able to change these so I set the attributes to editable=False for both.我知道用户不应该能够更改这些,所以我将两者的属性都设置为 editable=False。 However, it throws an error.但是,它会引发错误。 Any idea what I need to do to be able to display these two fields in my admin forms?知道我需要做什么才能在我的管理员 forms 中显示这两个字段吗?

This is my error: 'created' cannot be specified for Volunteer model form as it is a non-editable field.这是我的错误:不能为志愿者 model 表单指定“已创建”,因为它是不可编辑的字段。 Check fields/fieldsets/exclude attributes of class VolunteerAdmin.检查 class VolunteerAdmin 的字段/字段集/排除属性。

Thanks for your help.谢谢你的帮助。

i think you could just get rid of the fields line,add 'created', 'edited' to list of display if you want, usually you can see the field automatically in the detail of every volunteer,我认为您可以摆脱字段行,如果需要,将“已创建”、“已编辑”添加到显示列表中,通常您可以在每个志愿者的详细信息中自动查看该字段,

maybe because you did it manually it got stuck, in that case delete your last migration and database (sqlite) and re-do makemagrations and migrate.可能是因为您手动执行它卡住了,在这种情况下删除您的最后一个迁移和数据库(sqlite)并重新执行 makemagrations 和迁移。

also if you are usning a form in a forms.py to be used in a template you should exclude 'created',and 'edited', because you cant edit them.此外,如果您在 forms.py 中使用表单以在模板中使用,则应排除“已创建”和“已编辑”,因为您无法编辑它们。

You should consider adding created and edited in read-only fields like:您应该考虑在只读字段中添加创建编辑,例如:

readonly_fields = ('created','edited')

Complete code snippet:完整的代码片段:

class VolunteerAdmin(admin.ModelAdmin):
    fields = ('first_name', 'last_name', 'email_address', 'status','created','edited')
    list_display = ('first_name', 'last_name', 'email_address', 'status')
    list_editable = ('status',)
    readonly_fields = ('created','edited')
    list_filter = ('status',)
    search_fields = ('first_name', 'last_name')

admin.site.register(Volunteer, VolunteerAdmin)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM