我尝试调用创建客户端用户,因此我必须从客户端模型调用 create_client 方法来创建客户端用户,我可以创建超级用户但我不知道如何创建客户端用户我真的不明白它是如何工作的有人可以向我解释,我将不胜感激
视图.py:
def signup(request):
form = RegisterForm(request.POST or None)
if form.is_valid():
form.save()
return render(request, 'accounts/signup.html', {'form': form})
表格.py
class UserAdminCreationForm(forms.ModelForm):
"""
A form for creating new users. Includes all the required
fields, plus a repeated password.
"""
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = User
fields = ('email','full_name')
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super(UserAdminCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
class UserAdminChangeForm(forms.ModelForm):
"""A form for updating users. Includes all the fields on
the user, but replaces the password field with admin's
password hash display field.
"""
password = ReadOnlyPasswordHashField()
class Meta:
model = User
fields = ('email','full_name','password', 'active', 'admin')
def clean_password(self):
# Regardless of what the user provides, return the initial value.
# This is done here, rather than on the field, because the
# field does not have access to the initial value
return self.initial["password"]
class Form(forms.Form):
fullname = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placedholder': 'you full name !'}))
email = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': 'your email'}))
content = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'placeholder': ' content! '}))
def clean_email(self):
email = self.cleaned_data.get('email')
if not 'email.com' in email:
raise forms.ValidationError('Email has to be gmail ')
return email
class LoginForm(forms.Form):
username = forms.EmailField(label='Email')
password = forms.CharField(widget=forms.PasswordInput)
class RegisterForm(forms.ModelForm):
"""
A form for creating new users. Includes all the required
fields, plus a repeated password.
"""
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = User
fields = ('email', 'full_name')
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super(RegisterForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
user.active = True
user.client = True
if commit:
user.save()
return user
模型.py
class AccountManager(BaseUserManager):
def create_user(self,email,full_name=None,password=None , is_active=True,is_staff=False,is_admin=False,is_client=False):
"""
Creates and saves a User with the given email and password.
"""
if not email:
raise ValueError('Users must have an email address')
if not password:
raise ValueError('Users must have an password ')
user_object = self.model(
email=self.normalize_email(email),
full_name=full_name,
)
user_object.set_password(password)
user_object.staff =is_staff
user_object.admin = is_admin
user_object.active = is_active
user_object.client = is_client
user_object.save(using=self._db)
return user_object
def create_Client(self, email, e, password, full_name=None):
"""
Creates and saves a staff user with the given email and password.
"""
user = self.create_user(
email,
full_name=full_name,
password=password,
is_client=True
)
def create_staffuser(self, email,e, password,full_name=None):
"""
Creates and saves a staff user with the given email and password.
"""
user = self.create_user(
email,
full_name =full_name,
password=password,
is_staff=True
)
user.save(using=self._db)
return user
def create_superuser(self, email, password,full_name=None):
"""
Creates and saves a superuser with the given email and password.
"""
user = self.create_user(
email,
full_name = full_name,
password=password,
is_staff = True,
is_admin = True,
)
user.save(using=self._db)
return user
class Account(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=255,unique=True)
full_name = models.CharField(max_length=25,blank=True,null=True)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False) # a admin user; non super-user
admin = models.BooleanField(default=False) # a superuser
client = models.BooleanField(default=False) # a superuser
# notice the absence of a "Password field", that is built in.
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = AccountManager()
def get_full_name(self):
# The user is identified by their email address
if self.full_name:
return self.full_name
return self.email
def get_short_name(self):
# The user is identified by their email address
return self.email
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
def __str__(self): # __unicode__ on Python 2
return self.email
@property
def is_staff(self):
"Is the user a member of staff?"
return self.staff
@property
def is_admin(self):
"Is the user a admin member?"
return self.admin
@property
def is_active(self):
"Is the user active?"
return self.active
@property
def is_client(self):
"Is the user active?"
return self.client