我想知道是否有办法在用户进行更改时覆盖管理面板中的值?
方案: 我有一个带有两个字段的模型,last_edited_by和last_edited_date。 我想要做的是当用户对条目进行更改时自动填充这两个字段。
尝试: 这里有一些代码允许我打印其中一个字段的值,但即使应用了新值,管理面板也没有变化。
admin.py
class ProcessSheetRefAdmin(admin.ModelAdmin):
def change_view(self, request, object_id, form_url='', extra_context=None):
obj = super(ProcessSheetRefAdmin, self).get_object(request, object_id)
if obj is not None:
"""
attempting to update the value in last_edited_date,
but even after applying new value, the admin panel shows old value from
last database entry.
"""
obj.last_edited_date = time.time()
if request.method == 'POST':
print "admin post"
return super(ProcessSheetRefAdmin, self).change_view(request, object_id, form_url, extra_context)
admin.site.register(ProcessSheetRefrence, ProcessSheetRefAdmin)
任何帮助都将不胜感激!!
答案 0 :(得分:0)
当您对obj
进行更改时,需要在此之后保存。
if obj is not None:
"""
attempting to update the value in last_edited_date,
but even after applying new value, the admin panel shows old value from
last database entry.
"""
obj.last_edited_date = time.time()
obj.save()