Django自定义customUser模型仅存邮箱密码,姓名字段为空问题
Hey there, let's get to the bottom of why your firstname and lastname fields are staying empty when users register! The root cause is in your UserManager's create_user method—you're not assigning the passed-in first/last names to the user object before saving it to the database. Let's walk through the fixes:
1. Update the create_user Method in UserManager
Right now, when you create the user_obj instance, you only set the email. You need to add the firstname and lastname parameters to the model initialization. Here's the corrected code:
class UserManager(BaseUserManager): def create_user(self, email, firstname, lastname, password=None, is_active=True, is_staff=False, is_admin=False): if not email: raise ValueError("user must have an email") user_obj = self.model( email = self.normalize_email(email), firstname=firstname, # Add this line to save first name lastname=lastname # Add this line to save last name ) user_obj.set_password(password) user_obj.active = is_active user_obj.staff = is_staff user_obj.admin = is_admin user_obj.save(using=self._db) return user_obj # Keep the create_staffuser and create_superuser methods the same
2. Update REQUIRED_FIELDS in customUser
Since firstname and lastname are required for all users, you should add them to the REQUIRED_FIELDS list. This ensures Django's built-in tools (like the createsuperuser command) prompt for these fields when needed:
class customUser(AbstractBaseUser): email = models.EmailField(max_length=225, unique=True) firstname = models.CharField(max_length=225) lastname = models.CharField(max_length=225) active = models.BooleanField(default=True) staff = models.BooleanField(default=False) admin = models.BooleanField(default=False) USERNAME_FIELD = 'email' REQUIRED_FIELDS = ['firstname', 'lastname'] # Update this line objects = UserManager() # Keep all other methods the same
3. Simplify the Signup View (Optional but Recommended)
Your create_user method already calls save() internally, so you don't need to call user.save() again in your view. This removes redundant database operations:
def signup(request): if request.method == "POST": firstname1 = request.POST['firstname'] lastname1 = request.POST['lastname'] email1 = request.POST['email'] password1 = request.POST['password1'] password2 = request.POST['password2'] if password1 == password2: if customUser.objects.filter(email=email1).exists(): messages.info(request,'email already taken') return redirect('signup') else: # Remove the user.save() line below—create_user handles saving user = customUser.objects.create_user(email=email1, firstname=firstname1, lastname=lastname1, password=password1) return redirect('/') else: messages.info(request, 'Password Doesnt Match') return redirect('signup') else: return render(request, 'signup.html')
Why This Works:
- The first fix directly addresses the missing field assignment—now when you call
create_user, the first and last names are attached to the user object before it's saved to MySQL. - Updating
REQUIRED_FIELDSaligns your custom model with Django's expectations, preventing issues with admin commands or future extensions. - Removing the extra
save()call cleans up your code and avoids unnecessary database writes.
内容的提问来源于stack exchange,提问作者safwan

