I have a Django application hwere I have extended the User model and created a custom user model for registration/login, now I want to implement a Change Password API which will be used in Android/IOS app development. I would get parameters as:
user_id, old_password, new_password
Using these paramters and custom user model and serializer is there any way I could achieve this.
I have tried a sample example for this, but it failed.
Custom Model:
class User(AbstractBaseUser, PermissionsMixin):
objects = UserManager()
name = models.CharField(max_length=100, blank=True, null=True)
email = models.EmailField(unique=True)
created_at = models.DateField(blank=True, null=True, auto_now=True)
phone_no = models.CharField(max_length=14, blank=True, null=True)
user_android_id = models.CharField(max_length=255, blank=True, null=True)
user_fcm_token = models.CharField(max_length=255, blank=True, null=True)
user_social_flag = models.IntegerField(blank=True, null=True)
user_fb_id = models.CharField(max_length=255, blank=True, null=True)
user_android_app_version = models.CharField(max_length=25, blank=True, null=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
USERNAME_FIELD = 'email'
def __str__(self):
return self.email
User Manager:
class UserManager(BaseUserManager):
use_in_migrations = True
def create_user(self, email, name, phone_no, created_at, user_android_id, user_fcm_token,
user_social_flag, user_fb_id, user_android_app_version, password=None):
cache.clear()
user = self.model(
email=self.normalize_email(email),
phone_no=phone_no,
created_at=created_at,
user_android_id=user_android_id,
user_fcm_token=user_fcm_token,
user_social_flag=user_social_flag,
user_fb_id=user_fb_id,
user_android_app_version=user_android_app_version,
name=name,
)
user.is_admin = False
user.is_staff = True
user.is_superuser = False
user.set_password(password)
user.save(using=self._db)
return user
def create_staffuser(self, email, name, created_at, phone_no, user_android_id, user_fcm_token,
user_social_flag, user_fb_id, user_android_app_version, password):
cache.clear()
user = self.create_user(
email,
# password=password,
created_at=created_at,
phone_no=phone_no,
user_android_id=user_android_id,
user_fcm_token=user_fcm_token,
user_social_flag=user_social_flag,
user_fb_id=user_fb_id,
user_android_app_version=user_android_app_version,
name=name,
)
user.set_password(password)
user.is_staff = True
user.is_admin = False
user.is_superuser = False
user.save(using=self._db)
return user
def create_superuser(self, email, password):
cache.clear()
user = self.model(
email=self.normalize_email(email),
# password=password,
# phone_no=phone_no,
# created_at=created_at,
# user_android_id=user_android_id,
# user_fcm_token=user_fcm_token,
# user_social_flag=user_social_flag,
# user_fb_id=user_fb_id,
# user_android_app_version=user_android_app_version,
# name=name,
)
user.set_password(password)
user.is_admin = True
user.is_staff = False
user.is_superuser = True
user.save(using=self._db)
return user
Custom User Serializer:
class CustomRegisterSerializer(RegisterSerializer):
email = serializers.EmailField(required=False)
password1 = serializers.CharField(required=False, allow_null=True, allow_blank=True)
name = serializers.CharField(required=False, allow_null=True, allow_blank=True)
phone_no = serializers.CharField(required=False, allow_null=True, allow_blank=True)
user_android_id = serializers.CharField(required=False, allow_null=True, allow_blank=True)
user_fcm_token = serializers.CharField(required=False, allow_null=True, allow_blank=True)
user_social_flag = serializers.IntegerField(required=False, allow_null=True)
user_fb_id = serializers.CharField(required=False, allow_null=True, allow_blank=True)
user_android_app_version = serializers.CharField(required=False, allow_null=True, allow_blank=True)
# created_at = serializers.DateField(format="%Y-%m-%d", input_formats=['%Y-%m-%d', 'iso-8601'])
class Meta:
model = User
fields = ('email', 'password', 'name', 'phone_no', 'user_android_id', 'user_fcm_token',
'user_social_flag', 'user_fb_id', 'user_android_app_version')
def get_cleaned_data(self):
super(CustomRegisterSerializer, self).get_cleaned_data()
return {
'password1': self.validated_data.get('password1', ''),
'email': self.validated_data.get('email', ''),
'phone_no': self.validated_data.get('phone_no', ''),
'name': self.validated_data.get('name', ''),
'user_android_id': self.validated_data.get('user_android_id', ''),
'user_fcm_token': self.validated_data.get('user_fcm_token', ''),
'user_social_flag': self.validated_data.get('user_social_flag', ''),
'user_fb_id': self.validated_data.get('user_fb_id', ''),
'user_android_app_version': self.validated_data.get('user_android_app_version', ''),
}
def create(self, validated_data):
user = User.objects.create_user(**validated_data)
return user
ChangePassword API :
class CustomChangePasswordView(APIView):
"""
User Change Password API
"""
def post(self, request):
data = request.data
u_id = data.get('user_id')
old_password = data.get('user_old_password')
new_password = data.get('user_new_password')
user = User.objects.get(id=u_id)
if user.password != old_password:
return Response({"msg":"Invalid Old Password"}, status=status.HTTP_200_OK)
else:
user.set_password(new_password)
return Response({"msg":"Change successfull!"}, status=status.HTTP_201_CREATED)
Error Response I get:
{
"msg": "Invalid Old Password"
}
What am I missing?
Is there another better way for this?
Update in the CustomChangePasswordView code like this:
if user.check_password(old_password):
user.set_password(new_password)
user.save()
return Response({"msg":"Change successfull!"}, status=status.HTTP_201_CREATED)
else:
return Response({"msg":"Invalid Old Password"}, status=status.HTTP_200_OK)
You can check the documentation on check password in here.
Related
Context
I have a project in which there are three entities : Account, Community and JoinRequest.
A JoinRequest binds an Account (user) with a Community. And there should not be more than one JoinRequest for any couple (Account, Community).
Problem
I coded the respective models, serializers and unittest, which you can see below. But when I run my test, it fails when returning join_request_serializer.is_valid() = False
Which leads to the following error :
join_request_serializer.errors
{'user': {'email': [ErrorDetail(string='account with this email already exists.', code='unique')], 'username': [ErrorDetail(string='account with this username already exists.', code='unique')], 'password': [ErrorDetail(string='This field is required.', code='required')]}, 'community': {'name': [ErrorDetail(string='community with this name already exists.', code='unique')]}}
It seems the .is_valid() method of the JoinRequestSerializer tries to recreate an Account and a Community, whose data were previously passed as arguments at construction of the instance...
Any idea why this error appears?
Unit Test
class JoinRequestSerializerTestCase(TestCase):
def test_join_request_serializer_create_success(self):
account = register()
account_serializer = AccountSerializer(account)
community_data = {
'name': 'CommunityNameExample'
}
community = Community(community_data)
community_serializer = CommunitySerializer(community)
data = {'user':account_serializer.data, 'community':community_serializer}
join_request_serializer = JoinRequestSerializer(data=data)
self.assertEqual(join_request_serializer.is_valid(), True)
join_request_serializer.save()
self.assertEqual(JoinRequest.objects.count(), 1)
Account
class MyAccountManager(BaseUserManager):
def create_user(self, email, username, password=None):
if not email:
raise ValueError('Users must have an email address')
if not username:
raise ValueError('Users must have an username')
user = self.model(
email=self.normalize_email(email),
username=username,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, password):
# This method must be overridden to use MyAccountManager class
user = self.create_user(
email=self.normalize_email(email),
username=username,
password=password,
)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
username = models.CharField(
max_length=30,
unique=True,
validators = [
RegexValidator(
regex='^[a-zA-Z0-9]*$',
message='Username must be Alphanumeric.',
code='invalid_username'
)
]
)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now_add=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
USERNAME_FIELD = 'username' # Generic (not explicit) keyword for the login field
REQUIRED_FIELDS = ['email']
objects = MyAccountManager() # What is the point of this line?
def create(self, email, username, password, **kwargs):
account = Account.objects.create_user(
username = username,
email = email,
password = password
)
post_save(sender=Account, instance=account, created = True, raw=True)
return account
Community
class Community(models.Model):
name = models.CharField(
max_length=30,
unique=True,
validators = [
RegexValidator(
regex='^[a-zA-Z0-9]*$',
message='Community name must be Alphanumeric.',
code='invalid_username'
)
]
)
bio = models.CharField(max_length=150, blank=True, default='')
slug = models.SlugField(max_length=30, blank=True, default=slugify(name))
class Meta(object):
verbose_name_plural = 'Communities'
def __str__(self):
return self.name
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Community, self).save(*args, **kwargs)
JoinRequest
class JoinRequest(models.Model):
community = models.ForeignKey(Community, on_delete=models.CASCADE)
user = models.ForeignKey(Account, on_delete=models.CASCADE)
creation_date = models.DateTimeField(auto_now_add=True)
class Meta:
constraints = [
models.UniqueConstraint(fields=['community', 'user'], name='unique_joinrequest')
]
AccountSerializer
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = ['email', 'username', 'password']
extra_kwargs = {'password':{'write_only':True}}
def create(self):
account = Account.objects.create_user(
email = self.validated_data['email'],
username = self.validated_data['username'],
password = self.validated_data['password'],
)
return account
CommunitySerializer
class CommunitySerializer(serializers.ModelSerializer):
class Meta:
model = Community
fields = ['name', 'bio']
lookup_field = 'slug'
extra_kwargs = {
'url':{'lookup_field':'slug'}
}
JoinRequestSerializer
class JoinRequestSerializer(serializers.ModelSerializer):
user = AccountSerializer(
source='account_set',
)
community=CommunitySerializer(
source='community_set',
)
class Meta:
model = JoinRequest
fields = ['user', 'community']
read_only_fields = ('user', 'community')
validators = [
UniqueTogetherValidator(
queryset=JoinRequest.objects.all(),
fields=['user', 'community'],
message='A Join Request for this couple of User'\
' and Community already exists.'
)
]
extra_kwargs = {
'user':{'read_only':True},
'commnuity':{'read_only':True}
}
def create(self):
join_request = JoinRequest(
user = self.validated_data['user'],
community = self.validated_data['community'],
)
return join_request
So, this is probably just something simple I'm doing wrong here. But basically, I am attempting to simply pass in the pk of the other_user. I tested that the query works correctly, and am indeed printing the other user. Just don't know where to place it correctly in my code so I can pass it to the render part.
local variable 'other_user' referenced before assignment
models.py
class ProfileManager(BaseUserManager):
def create_user(self, username, email,description,photo, password=None):
if not email:
raise ValueError("You must creat an email")
if not username:
raise ValueError("You must create a username!")
if not description:
raise ValueError("You must write a description")
if not photo:
raise ValueError("You must upload a photo")
user = self.model(
email=self.normalize_email(email),
username = username,
description= description,
photo= photo,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email,description,photo, password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
username=username,
description=description,
photo=photo,
)
user.is_admin=True
user.is_staff=True
user.is_superuser=True
user.save(using=self._db)
return user
class Profile(AbstractBaseUser):
class Meta:
swappable = 'AUTH_USER_MODEL'
email = models.EmailField(verbose_name="email")
username = models.CharField(max_length=30, unique=True)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
#what I added
description = models.TextField()
photo = models.ImageField(upload_to='profile_photo',blank=False, height_field=None, width_field=None, max_length=100)
matches = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='+', blank=True)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['description','photo','email']
objects = ProfileManager()
def __str__(self):
return self.username
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self,app_label):
return True
class Conversation(models.Model):
members = models.ManyToManyField(settings.AUTH_USER_MODEL)
class UserVote(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
voter = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='given_vote', on_delete=models.CASCADE)
vote = models.BooleanField(default=False)
class Meta:
unique_together = (('user', 'voter'))
class InstantMessage(models.Model):
sender = models.ForeignKey(settings.AUTH_USER_MODEL, related_name= 'sender',on_delete=models.CASCADE )
conversation = models.ForeignKey(Conversation, on_delete=models.CASCADE)
message = models.TextField()
date = models.DateTimeField(verbose_name="Data creation",default=timezone.now(), null=False)
def __unicode__(self):
return self.message
#tests to see if messages are exclusive between sender, receiver (won't work with new model)
#classmethod
def find_messages_exclusive_to_profile(cls,sender,receiver):
#members = receiver AND sender, not receiver or sender
exclusive_conversations = Conversation.objects.filter(members= receiver ).filter(members= sender)
exclusive_messages = InstantMessage.objects.filter(conversation__in=exclusive_conversations)
return exclusive_messages
def message (request, profile_id):
if request.method == 'POST':
form = MessageForm(request.POST)
if form.is_valid():
form.save()
return redirect('dating_app:messages', other_user.id)
else:
conversation, created = Conversation.objects.filter(members = request.user).filter(members= profile_id).get_or_create()
print(conversation)
other_user = conversation.members.filter(id=profile_id).get()
print(other_user)
form = MessageForm({'sender': request.user, 'conversation': conversation})
context = {'form' : form }
return render(request, 'dating_app/message.html', context)
You can access the ModelForm's instance from form.instance and get value from there.
if form.is_valid():
form.save()
m = form.instance
return redirect('dating_app:messages', m.id)
You are mostly likely to also get the instance while saving it as
m = form.save()
I am trying to create a custom user model in Django and thereafter create RESTAPI as per django-rest-auth provides.
CustomUserModel and CustomUSerManager are defined as-
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
class UserManager(BaseUserManager):
use_in_migrations = True
def create_user(self, email, name, phone_no, user_android_id, user_fcm_token,
user_social_flag, user_fb_id, user_android_app_version, password=None):
user = self.model(
email = self.normalize_email(email),
phone_no = phone_no,
password=password,
user_android_id = user_android_id,
user_fcm_token = user_fcm_token,
user_social_flag = user_social_flag,
user_fb_id = user_fb_id,
user_android_app_version = user_android_app_version,
name = name,
)
# user.is_staff = False
# user.is_superuser = True
user.set_password(password)
user.save(using=self._db)
return user
def create_staffuser(self, email, name, phone_no, user_android_id, user_fcm_token,
user_social_flag, user_fb_id, user_android_app_version, password):
user = self.create_user(
email,
password=password,
phone_no=phone_no,
user_android_id=user_android_id,
user_fcm_token=user_fcm_token,
user_social_flag=user_social_flag,
user_fb_id=user_fb_id,
user_android_app_version=user_android_app_version,
name=name,
)
user.is_staff = True
user.is_admin = False
user.is_superuser = True
user.save(using=self._db)
return user
def create_superuser(self, email, name, phone_no, user_android_id, user_fcm_token,
user_social_flag, user_fb_id, user_android_app_version, password):
user = self.create_user(
email,
password=password,
phone_no=phone_no,
user_android_id=user_android_id,
user_fcm_token=user_fcm_token,
user_social_flag=user_social_flag,
user_fb_id=user_fb_id,
user_android_app_version=user_android_app_version,
name=name,
)
user.is_admin = True
user.save(using=self._db)
return user
class User(AbstractBaseUser):
objects = UserManager()
name = models.CharField(max_length=100, blank=True, null=True)
email = models.EmailField(unique=True)
created_at = models.DateField(blank=True, null=True)
phone_no = models.BigIntegerField(blank=True, null=True)
user_android_id = models.CharField(max_length=255, blank=True, null=True)
user_fcm_token = models.CharField(max_length=255, blank=True, null=True)
user_social_flag = models.IntegerField(blank=True, null=True)
user_fb_id = models.CharField(max_length=255, blank=True, null=True)
user_android_app_version = models.CharField(max_length=25, blank=True, null=True)
USERNAME_FIELD = 'email'
def __str__(self):
return self.email
Custom View File:
from rest_auth.registration.views import RegisterView
from app.models import User
class CustomRegisterView(RegisterView):
queryset = User.objects.all()
In settings.py file I have set:
AUTH_USER_MODEL = 'app.User'
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_VERIFICATION = 'none'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_USER_EMAIL_FIELD = 'email'
ACCOUNT_LOGOUT_ON_GET = True
REST_AUTH_SERIALIZERS = {
"USER_DETAILS_SERIALIZER":
"app.serializers.CustomUserDetailsSerializer",
}
REST_AUTH_REGISTER_SERIALIZERS = {
"REGISTER_SERIALIZER":
"app.serializers.CustomRegisterSerializer",
}
I have set the urls in url file.
re_path(r'^registration/$', views.CustomRegisterView.as_view())
When I run the /registration/ url, and fill all the details I get error:
django.db.utils.IntegrityError: null value in column "is_superuser" violates not-null constraint
DETAIL: Failing row contains (8, pbkdf2_sha256$120000$VpGItoUPHoJ9$yBQB2PqRDiqd4SQ2cDZX/wzaV3yFSf..., null, null, null, null, null, null, null, null, null, custom#user.com, null, null, null, null, null, null, null).
What am I missing here?
UPDATE 1:
While checking the database records, I found that only email, password, and last_login fields are only inserted and rest all are null. What could be reason?
UPDATE 2:
CustomRegisterSerializer class:-
class CustomRegisterSerializer(RegisterSerializer):
email = serializers.EmailField()
password1 = serializers.CharField(write_only=True)
name = serializers.CharField()
phone_no = serializers.IntegerField()
user_android_id = serializers.CharField()
user_fcm_token = serializers.CharField(required=True)
user_social_flag = serializers.IntegerField()
user_fb_id = serializers.CharField(required=True)
user_android_app_version = serializers.CharField()
class Meta:
model = User
fields = ('email', 'name', 'phone_no', 'user_android_id', 'user_fcm_token',
'user_social_flag', 'user_fb_id', 'user_android_app_version')
def get_cleaned_data(self):
super(CustomRegisterSerializer, self).get_cleaned_data()
return {
'password1': self.validated_data.get('password1', ''),
'email': self.validated_data.get('email', ''),
'phone_no': self.validated_data.get('phone_no'),
'name': self.validated_data.get('name'),
'user_android_id': self.validated_data.get('user_android_id'),
'user_fcm_token': self.validated_data.get('user_fcm_token'),
'user_social_flag': self.validated_data.get('user_social_flag'),
'user_fb_id': self.validated_data.get('user_fb_id'),
'user_android_app_version': self.validated_data.get('user_android_app_version'),
}
In create_user you've commented is_staff and is_superuser. Uncomment and set them to False.
EDIT:
About the fields not getting into database - is_staff and is_superuser are not defined in your user model and are also not defined in the AbstractBaseUser. Which is strange, because in this case you shouldn't have gotten that exception in the first place.
Are you just now starting your project or have you ran migrations before swapping the user model?
If you've started with the default user model from Django, you'll need some more work to do. Hopefully you don't have any real users yet and you could do it quick.
Take a careful look at the topic in Django docs.
I have a django rest project wherein I have customized user model to fit my needs. I also have created a custom serializer to add more fields like address, phone number etc to be stored during registration. I am using django-rest-auth for login and registration. Browsable API looks like follows:
The registration is successful, however it fails to store address and user type.
My custom user model:
class UserManager(BaseUserManager):
def _create_user(self, username, email, password, is_staff, is_superuser, **extra_fields):
now = timezone.now()
if not username:
raise ValueError(_('The given username must be set'))
email = self.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=is_staff, is_active=True,
is_superuser=is_superuser, last_login=now,
date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, username, email=None, password=None, **extra_fields):
return self._create_user(username, email, password, False, False,
**extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
user=self._create_user(username, email, password, True, True,
**extra_fields)
user.is_active=True
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField(_('username'), max_length=30, unique=True,
help_text=_('Required. 30 characters or fewer. Letters, numbers and #/./+/-/_ characters'),
validators=[
validators.RegexValidator(re.compile('^[\w.#+-]+$'), _('Enter a valid username.'), _('invalid'))
])
first_name = models.CharField(_('first name'), max_length=30, blank=True, null=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True, null=True)
email = models.EmailField(_('email address'), max_length=255, unique=True)
is_staff = models.BooleanField(_('staff status'), default=False,
help_text=_('Designates whether the user can log into this admin site.'))
is_active = models.BooleanField(_('active'), default=True,
help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
receive_newsletter = models.BooleanField(_('receive newsletter'), default=False)
birth_date = models.DateField(_('birth date'), auto_now=False, null=True)
address = models.CharField(_('address'), max_length=30, blank=True, null=True)
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
phone_number = models.CharField(_('phone number'), validators=[phone_regex], max_length=30, blank=True, null=True) # validators should be a list
USER_TYPES = (
('Farmer', 'Farmer'),
('Windmill owner', 'Windmill owner'),
('Solar panel owner', 'Solar panel owner'),)
user_type = models.CharField(_('user type'), choices=USER_TYPES, max_length=30, blank=True, null=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username',]
objects = UserManager()
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
def get_full_name(self):
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
return self.first_name
def email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self.email])
My register serializer:
class RegisterSerializer(serializers.Serializer):
email = serializers.EmailField(required=allauth_settings.EMAIL_REQUIRED)
first_name = serializers.CharField(required=True, write_only=True)
last_name = serializers.CharField(required=True, write_only=True)
address = serializers.CharField(max_length=500, required=True, write_only=True)
#user_type = serializers.CharField(required=True, write_only=True)
user_type = serializers.ChoiceField(
choices=[('Farmer', 'Farmer'),('Windmill owner', 'Windmill owner'),('Solar panel owner', 'Solar panel owner'),],
style={'base_template': 'radio.html'},
required=True, write_only=True)
password1 = serializers.CharField(required=True, write_only=True)
password2 = serializers.CharField(required=True, write_only=True)
def validate_email(self, email):
email = get_adapter().clean_email(email)
if allauth_settings.UNIQUE_EMAIL:
if email and email_address_exists(email):
raise serializers.ValidationError(
_("A user is already registered with this e-mail address."))
return email
def validate_password1(self, password):
return get_adapter().clean_password(password)
def validate(self, data):
if data['password1'] != data['password2']:
raise serializers.ValidationError(
_("The two password fields didn't match."))
return data
def get_cleaned_data(self):
return {
'first_name': self.validated_data.get('first_name', ''),
'last_name': self.validated_data.get('last_name', ''),
'address': self.validated_data.get('address', ''),
'user_type': self.validated_data.get('user_type', ''),
'password1': self.validated_data.get('password1', ''),
'email': self.validated_data.get('email', ''),
}
def save(self, request):
adapter = get_adapter()
user = adapter.new_user(request)
self.cleaned_data = self.get_cleaned_data()
adapter.save_user(request, user, self)
setup_user_email(request, user, [])
user.save()
return user
What exactly is going wrong? Why these fields are not stored?
I am using DRF and for login/registration I am using Django-rest-auth.
I have customized User model to have extra fields
I have custom registration serializer to store extra fields along with username, password while registering a new user.
Registration is successful however, extra fields are not saved along with username, first_name, last_name and password.
My model:
class UserManager(BaseUserManager):
def _create_user(self, username, email, password, is_staff, is_superuser, address, **extra_fields):
now = timezone.now()
if not username:
raise ValueError(_('The given username must be set'))
email = self.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=is_staff, is_active=True,
is_superuser=is_superuser, last_login=now,
date_joined=now, address=address, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, username, email=None, password=None, **extra_fields):
return self._create_user(username, email, password, False, False, True,
**extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
user=self._create_user(username, email, password, True, True,
**extra_fields)
user.is_active=True
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField(_('username'), max_length=30, unique=True,
help_text=_('Required. 30 characters or fewer. Letters, numbers and #/./+/-/_ characters'),
validators=[
validators.RegexValidator(re.compile('^[\w.#+-]+$'), _('Enter a valid username.'), _('invalid'))
])
first_name = models.CharField(_('first name'), max_length=30, blank=True, null=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True, null=True)
email = models.EmailField(_('email address'), max_length=255, unique=True)
is_staff = models.BooleanField(_('staff status'), default=False,
help_text=_('Designates whether the user can log into this admin site.'))
is_active = models.BooleanField(_('active'), default=True,
help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
receive_newsletter = models.BooleanField(_('receive newsletter'), default=False)
birth_date = models.DateField(_('birth date'), auto_now=False, null=True)
address = models.CharField(_('address'), max_length=30, blank=True, null=True)
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
phone_number = models.CharField(_('phone number'), validators=[phone_regex], max_length=30, blank=True, null=True) # validators should be a list
USER_TYPES = (
('Farmer', 'Farmer'),
('Windmill owner', 'Windmill owner'),
('Solar panel owner', 'Solar panel owner'),)
user_type = models.CharField(_('user type'), choices=USER_TYPES, max_length=30, blank=True, null=True)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email',]
objects = UserManager()
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
def get_full_name(self):
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
return self.first_name
def email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self.email])
My Serializer:
class RegisterSerializer(serializers.Serializer):
email = serializers.EmailField(required=allauth_settings.EMAIL_REQUIRED)
first_name = serializers.CharField(required=True, write_only=True)
last_name = serializers.CharField(required=True, write_only=True)
address = serializers.CharField(required=True, write_only=True)
user_type = serializers.ChoiceField(
choices=(('Farmer', 'Farmer'),('Windmill owner', 'Windmill owner'),('Solar panel owner', 'Solar panel owner'),),
style={'base_template': 'radio.html'},
required=True, write_only=True)
password1 = serializers.CharField(required=True, write_only=True)
password2 = serializers.CharField(required=True, write_only=True)
def validate_email(self, email):
email = get_adapter().clean_email(email)
if allauth_settings.UNIQUE_EMAIL:
if email and email_address_exists(email):
raise serializers.ValidationError(
_("A user is already registered with this e-mail address."))
return email
def validate_password1(self, password):
return get_adapter().clean_password(password)
def validate(self, data):
if data['password1'] != data['password2']:
raise serializers.ValidationError(
_("The two password fields didn't match."))
return data
def get_cleaned_data(self):
return {
'first_name': self.validated_data.get('first_name', ''),
'last_name': self.validated_data.get('last_name', ''),
'address': self.validated_data.get('address', ''),
'user_type': self.validated_data.get('user_type', ''),
'password1': self.validated_data.get('password1', ''),
'email': self.validated_data.get('email', ''),
}
def save(self, request):
adapter = get_adapter()
user = adapter.new_user(request)
self.cleaned_data = self.get_cleaned_data()
adapter.save_user(request, user, self)
setup_user_email(request, user, [])
user.save()
return user
What is wrong?
It seems like django-allauth doesn't allow saving custom fields by default:
(ref: https://github.com/pennersr/django-allauth/blob/master/allauth/account/adapter.py#L227)
To go around it, simply assign the custom field values before doing user.save()
self.cleaned_data = self.get_cleaned_data()
adapter.save_user(request, user, self)
setup_user_email(request, user, [])
user.address = self.cleaned_data.get('address')
user.user_type = self.cleaned_data.get('user_type')
user.save()
return user
That was a dirty fix. A cleaner way would be to override the allauth adapter to support your custom fields.
To override the default adapter and save the custom fields try the following
Create an adapters.py file in your app root folder and paste the code below
from allauth.account.adapter import DefaultAccountAdapter
class CustomUserAccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=True):
"""
Saves a new `User` instance using information provided in the
signup form.
"""
from allauth.account.utils import user_field
user = super().save_user(request, user, form, False)
user_field(user, 'address', request.data.get('address', ''))
user_field(user, 'first_name', request.data.get('first_name', ''))
user_field(user, 'last_name', request.data.get('last_name', ''))
user_field(user, 'user_type', request.data.get('user_type', ''))
user.save()
return user
Lastly set the settings configuration to use your custom adapter by adding this line in the settings.py file
ACCOUNT_ADAPTER = 'users.adapters.CustomUserAccountAdapter'