I want to exclude a field in a CustomUser modelform which should be part of the BaseInlineFormset. What I tried:
class CustomUserForm(forms.ModelForm):
model = CustomUser
class Meta:
exclude = ('send_creation_mail',)
def clean(self):
cleaned_department = self.cleaned_data['department']
cleaned_report_to = self.cleaned_data['report_to']
# count = 0
if cleaned_department is None:
raise forms.ValidationError('Department is mandatory')
if cleaned_report_to is None:
raise forms.ValidationError('Report to is mandatory')
class CustomUserFormSet(BaseInlineFormSet):
model = CustomUser
form = CustomUserForm
def __init__(self, *args, **kwargs):
super(CustomUserFormSet, self).__init__(*args, **kwargs)
for form in self.forms:
form.empty_permitted = False
def clean(self):
# get forms that actually have valid data
count = 0
for form in self.forms:
try:
if form.cleaned_data and not form.cleaned_data.get(
'DELETE', False):
count += 1
if form.cleaned_data.get('department') is None:
raise forms.ValidationError('Department is mandatory')
if form.cleaned_data.get('report_to') is None:
raise forms.ValidationError('Reporting is mandatory')
except AttributeError:
# annoyingly, if a subform is invalid Django explicity raises
# an AttributeError for cleaned_data
pass
if count < 1:
raise forms.ValidationError('At least one detail is mandatory')
class CustomUserInline(admin.TabularInline):
formset = CustomUserFormSet
If I use CustomUserForm in CustomUserInline class, it definitely excludes the attribute 'send_creation_mail' but while saving the User form, it throws up an exception:
POST
Request URL: http://localhost:8000/dms/auth/user/add/
Django Version: 1.6.5
Exception Type: TypeError
Exception Value:
argument of type 'NoneType' is not iterable
I want to ensure that atleast 1 inline form is filled before saving the User details in the django admin auth. Using form = CustomUserForm, has no effect in the class CustomUserFormSet as the CustomUserForm displays all the fields of the Model(which should exclude 'send_creation_mail'). I need detailed code as to where there is a mistake?
您的self.cleaned_data
.clean()
不返回self.cleaned_data
(它需要返回cleaned_data
或引发ValidationError()
)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.