答案 0 :(得分:1)
试试这个:
from django.contrib.auth import forms
import datetime
class AddDaysForm(forms.ModelForm):
add_extra_days_to_subscription_expiry = forms.IntegerField(required=False)
class Meta:
fields = '__all__'
def __init__(self, *args, **kwargs):
instance = kwargs.get('instance')
if instance:
self.base_fields['add_extra_days_to_subscription_expiry'] = 0
forms.ModelForm.__init__(self, *args, **kwargs)
class SubscriptionAdmin(admin.ModelAdmin)
form = AddDaysForm
list_display = ('id', 'domain', 'domain_created',)
def save_model(self, request, obj, form, change):
if form.cleaned_data['add_extra_days_to_subscription_expiry'] != 0:
obj.subscription_expire = obj.subscription_expire + datetime.timedelta(days=form.cleaned_data['add_extra_days_to_subscription_expiry'])
obj.save()
使用此解决方案,您只需输入一个数字,然后通过管理员点击保存,它将为您添加。当表单重新加载时,它将重置为0,以便您可以输入新值。请注意,它还会考虑何时从默认情况下保存表单' 0'值。