嗨,我试图在 django 中实现多个用户类型,我在帐户模型和客户端模型之间存在一对一的关系,它不起作用,我不知道如何将用户从 Account 用户获取到 Client 用户我得到了这个错误('Client' 对象没有属性 'id')任何解决方案,拜托。
模型.py:
class AccountManager(BaseUserManager):
def create_user(self,email,full_name=None,password=None , is_active=True,is_staff=False,is_admin=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.save(using=self._db)
return user_object
def create_client(self,salary,email,password,full_name=None):
user = self.create_user(
email,
full_name=full_name,
password=password,
)
user.save(using=self._db)
return user
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
# 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
class Client(models.Model):
user = models.OneToOneField(Account,on_delete=models.CASCADE, primary_key=True,parent_link=True)
salary = models.DecimalField(default=0.00,max_digits=50,decimal_places=2)
objects = AccountManager()
def __unicode__(self):
return self.id
表格.py:
class RegisterClientForm(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)
salary = forms.DecimalField( max_digits=50, decimal_places=2)
class Meta:
model = Client
fields = ('salary',)
def clean_slary(self):
salary = self.cleaned_data.get('salary')
return salary
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(RegisterClientForm, self).save(commit=False)
user.password = self.cleaned_data["password1"]
# user.id = User.objects.get(id=user.id) => the problem is here ?
user.active = True
if commit:
user.save()
return user
视图.py:
def signup_client(request):
form = RegisterClientForm(request.POST or None)
if form.is_valid():
# form.client_save()
form.save()