我在 admin.py 中注冊了一個模型:
class OrderAdmin(admin.ModelAdmin):
list_display = ('org_name', 'address', 'total_cost', 'phone', 'data_time', 'is_called', 'is_payed')
search_fields = ('org_name', 'phone')
list_filter = ('data_time', 'total_cost', 'data_time')
list_editable = ('is_called', 'is_payed')
readonly_fields = ('data_time', 'user', 'total_cost')
inlines = [OrderItemsAdmin, ]
我需要做類似的事情:
class OrderAdmin(admin.ModelAdmin):
list_display = ('org_name', 'address', 'total_cost', 'phone', 'data_time', 'is_called', 'is_payed')
search_fields = ('org_name', 'phone')
list_filter = ('data_time', 'total_cost', 'data_time')
list_editable = ('is_called', 'is_payed')
readonly_fields = ('data_time', 'user', 'total_cost')
inlines = [OrderItemsAdmin, ]
if 'is_called' == True:
readonly_fields.append('is_called')
我認為這是可能的,所以問題是如何做到這一點?
uj5u.com熱心網友回復:
您可以使用ModelAdmin.get_readonly_fields()方法
試試這個
class OrderAdmin(admin.ModelAdmin):
...
def get_readonly_fields(self, request, obj=None):
readonly_fields = super(OrderAdmin, self).get_readonly_fields(request, obj)
if obj.is_called:
readonly_fields.append("is_called")
return readonly_fields
return readonly_fields
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/482992.html
