Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyUserAdminForm(forms.ModelForm):
- class Meta:
- model = User
- def __init__(self, *args, **kwargs):
- super(MyUserAdminForm, self).__init__(*args, **kwargs)
- instance = getattr(self, 'instance', None)
- if instance and instance.id: # username and user id
- self.fields['username'].widget.attrs['class'] = 'disabled'
- self.fields['username'].widget.attrs['readonly'] = True
- self.fields['is_superuser'].widget.attrs['class'] = 'disabled'
- self.fields['is_superuser'].widget.attrs['readonly'] = True
- new_text = '. <br \><span style="color: red;">Read-only field.\
- To modify, do this via shell.</span>'
- self.fields['username'].help_text += new_text
- self.fields['is_superuser'].help_text += new_text
- if instance.is_superuser is True:
- self.fields['is_active'].widget.attrs['class'] = 'disabled'
- self.fields['is_active'].widget.attrs['readonly'] = 'readonly'
- new_text = '. <br \><span style="color: red;">This option\
- is disabled for superuser. Do it via shell.</span>'
- self.fields['is_active'].help_text += new_text
- def clean_username(self):
- return self.instance.username
- def clean_is_superuser(self):
- return self.instance.is_superuser
- def clean_is_active(self):
- if self.instance.is_superuser is True:
- return self.instance.is_active
- else:
- return self.cleaned_data
- def save(self, *args, **kwargs):
- kwargs['commit'] = True
- user = super(MyUserAdminForm, self).save(*args, **kwargs)
- print user.username
- print 'done'
- return user
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement