You can do something like:IN:class CustomUserCreationForm(forms.ModelForm):
    ....
    def clean(self):        cleaned_data = super().clean()        # put your 
custom validator here!
   On Tuesday, May 11, 2021, 6:53:24 PM GMT+6, Ayush Bisht 
<bisht.ayush2...@gmail.com> wrote:  
 
 # admin.pyclass CustomUserAdmin(UserAdmin):    model = CustomUser    add_form 
= CustomUserCreationForm    fieldsets = (
        *UserAdmin.fieldsets, (            'User type',{                
'fields' : (                    'is_admin',                    'is_doctor',     
               'is_nurse',                                    ),                
'description': "Select any one of them "            }        )    )
# models.py
class CustomUser(AbstractUser):    username = models.CharField(max_length=50, 
unique=True, blank=False, null=False)       is_doctor = 
models.BooleanField(default=False, blank=False, null=False)    is_nurse = 
models.BooleanField(default=False, blank=False, null=False)            is_admin 
= models.BooleanField(default=False, blank=False, null=False)    
# forms.py
class CustomUserCreationForm(forms.ModelForm):    """    A form that creates a 
user, with no privileges, from the given username and    password.    """    
error_messages = {        'password_mismatch': _('The two password fields 
didn’t match.'),    }    password1 = forms.CharField(        
label=_("Password"),        strip=False,        
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),        # 
help_text=password_validation.password_validators_help_text_html(),    )    
password2 = forms.CharField(        label=_("Password confirmation"),        
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),        
strip=False,        help_text=_("Enter the same password as before, for 
verification."),    )     class Meta:        model = CustomUser        # fields 
= "('username',  'password' ,'password2', 'userchoice')"        fields = 
("username",)        # field_classes = {'username': UsernameField}
    def __init__(self, *args, **kwargs):        super().__init__(*args, 
**kwargs)             self.fields['username'].help_text = 'Enter you Staff ID'

    def clean_password2(self):        password1 = 
self.cleaned_data.get("password1")        password2 = 
self.cleaned_data.get("password2")        if password1 and password2 and 
password1 != password2:            raise ValidationError(                
self.error_messages['password_mismatch'],                
code='password_mismatch',            )        return password2

    def save(self, commit=True):        user = super().save(commit=False)       
 user.set_password(self.cleaned_data["password1"])        if commit:            
user.save()        return user



..............
the is_doctor , is_nurse   and is_admin fields comes at the userchageform.. is 
there a way to validate the fields at admin level.  I mean if someone , checked 
more than one designation then , a field error have to appear on the admin 
userchangeform,   Is there any way to customize the userchangeform...
Any help would be highly appreciable , 



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/da01a016-26fc-4590-992d-eaf86a502d7an%40googlegroups.com.
  

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1660007552.1091046.1620738931856%40mail.yahoo.com.

Reply via email to