Added clean_username function to validate form.

This commit is contained in:
Marcus Scholz 2020-12-25 12:24:09 +01:00
parent 67a6517716
commit cc59254ba4
2 changed files with 8 additions and 1 deletions

View File

@ -48,6 +48,14 @@ class ExplorerUserChangeForm(UserChangeForm):
self.fields['username'].required = False
self.fields['username'].help_text = None
self.fields['username'].widget.attrs['disabled'] = 'disabled'
def clean_username(self):
# As shown in the above answer.
instance = getattr(self, 'instance', None)
if instance:
return instance.username
else:
return self.cleaned_data.get('username', None)
class ExplorerChangeForm(forms.ModelForm):
class Meta:

View File

@ -60,7 +60,6 @@ class ExplorerProfileUpdateView(IsAuthenticatedMixin, View):
explorer_change_form = ExplorerChangeForm(request.POST, instance=request.user.explorer)
if explorer_change_form.is_valid() and explorer_user_change_form.is_valid():
print(explorer_user_change_form)
explorer_user_change_form.save()
explorer_change_form.save()