目前我需要从更改视图中删除一些字段,如果它的值为无
所以我有一个取款模型的管理员,如下所示:
class WithdrawalsAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(WithdrawalsAdminForm, self).__init__(*args, **kwargs)
none_fields = []
for single_field in self.fields:
self.fields[single_field].disabled = True
if self.initial[single_field] == None:
none_fields.append(single_field)
if none_fields:
for single_none_field in none_fields:
self.fields.pop(single_none_field)
class WithdrawalsAdmin(admin.ModelAdmin):
list_display = ('get_email', 'get_phone_number', 'currency', 'method', 'amount', 'status', 'created')
list_display_links = ('get_email', 'get_phone_number', 'currency', 'method', 'amount', 'status', 'created')
list_filter = [WithdrawalsStatusFilter] #just a custom list filter
change_form_template = 'admin/backend/withdrawals_change_form.html'
form = WithdrawalsAdminForm
fields = (
'id',
'get_email',
'get_phone_number',
'get_full_name',
'currency',
'method',
'to_bank',
'to_address',
'to_card',
'amount',
'status',
'transaction_id',
'created',
'submitted',
'confirmed',
)
readonly_fields = (
'id',
'get_email',
'get_phone_number',
'get_full_name',
'created',
)
def has_add_permission(self, request, obj=None):
return False
def get_queryset(self, request):
qs = super(WithdrawalsAdmin, self).get_queryset(request)
return qs.filter(Q(method=1) | Q(method=4))
但它返回给我以下错误:
“在‘WithdrawalsForm’中找不到键‘to_address’。选项有:金额、货币、方法、状态、to_bank。”
金额、货币、方法、状态、to_bank 在对象中的值都不是 None
to_address 值是 None,我还尝试打印出任何没有的字段,if self.initial[single_field] == None:并得到正确的字段名称(其中包括 to_address)具有 None 值。
如何从更改视图中排除具有 None 值的字段?
拉莫斯之舞
繁星coding
相关分类