我在admin.py中有一个带有以下类的django应用程序:
class SoftwareVersionAdmin(ModelAdmin):
fields = ("product", "version_number", "description",
"media", "relative_url", "current_version")
list_display = ["product", "version_number", "size",
"current_version", "number_of_clients", "percent_of_clients"]
list_display_links = ("version_number",)
list_filter = ['product',]
我想将这些文件用于添加页面,但将不同的字段用于更改页面。我怎样才能做到这一点?
最佳答案
首先看看位于get_form
的ModelAdmin类的get_formsets
和django.contrib.admin.options.py
方法的源代码。您可以覆盖这些方法,并使用kwargs获得所需的行为。例如:
class SoftwareVersionAdmin(ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
# Proper kwargs are form, fields, exclude, formfield_callback
if obj: # obj is not None, so this is a change page
kwargs['exclude'] = ['foo', 'bar',]
else: # obj is None, so this is an add page
kwargs['fields'] = ['foo',]
return super(SoftwareVersionAdmin, self).get_form(request, obj, **kwargs)
关于python - 在管理员中添加和更改页面的不同字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6321916/