5

I am trying to use email for authenication in Django. In other words I am extending django authentication. I followed all the steps and trust me it worked before. Now it stopped working.

this is model.py


    from django.db import models
    from django.contrib.auth.models import AbstractBaseUser,BaseUserManager,PermissionsMixin
    from django.core.mail import send_mail
    from django.utils.translation import ugettext_lazy as _
    #now=time.strftime('%Y-%M-%D %H:%m:%S.%u%Z')
    import datetime
    from datetime import timedelta

    from django.utils import timezone
    from django.utils.timezone import now


    tomorrow = timezone.now() + timedelta(days=1)
    current_time= timezone.now()

    class CustomUserManager(BaseUserManager):
        def _create_user(self,email,password,is_staff,is_superuser, **extra_fields):

            if not email:
                raise ValueError('The given email must be set')

            email=self.normalize_email(email)
            user= self.model(email=email,
                             is_staff=is_staff,
                             is_active = True,
                             is_superuser =is_superuser,
                             last_login=timezone.now(),
                             date_joined=timezone.now(),
                            **extra_fields)
            user.set_password(password)
            user.save(using=self._db)
            return user

        def create_user(self, email,password=None,**extra_fields):
            return self._create_user(email,password,False,False,**extra_fields)

        def create_superuser(self, email,password,**extra_fields):
            return self._create_user(email,password,True,True,**extra_fields)

    class CustomUser(AbstractBaseUser,PermissionsMixin):
        username =models.CharField(max_length =255, unique = True,blank = True,null= True)
        email =models.EmailField(blank=False, unique =True)
        date_joined  = models.DateTimeField(_('date joined'), default=now)
        is_active    = models.BooleanField(default=True)
        is_admin     = models.BooleanField(default=False)
        is_staff     = models.BooleanField(default=False)
        is_superuser = models.BooleanField(default=False)

        USERNAME_FIELD ='email'
        REQUIRED_FIELD =['user_name','date_joined']

        objects=CustomUserManager()

        class Meta:
            verbose_name=_('user')
            verbose_name_plural=_('users')

        def get_absolute_url(self):
            return "/user/%s" %urlquote(self.email)

        def get_full_name(self):

            a=UserProfile.objects.get(email_id=self.id)
            self.first_name=a.first_name
            self.last_name= a.last_name
            if not self.first_name and not self.last_name:
                full_name =self.email
            else:
                full_name = '%s %s' %(self.first_name,self.last_name)
            return full_name.strip()

        def get_short_name(self):
            self.first_name='a'
            return self.first_name

        def email_user(self,subject,message,from_email=None):
            send_mail(subject,message,from_email,[self.email])


            #code

    class UserProfile(models.Model):

        email = models.OneToOneField(CustomUser,unique =True,primary_key=True)
        first_name=models.CharField(max_length =256, blank = True)
        last_name=models.CharField(max_length =256, blank = True)
        activation_key = models.CharField(max_length=40,blank=True)
        gender = models.CharField(max_length=6, null=True,blank=True,choices=(
            ('male', 'Male'),
            ('female', 'Female'),))
        date_of_birth=models.DateField(null=True,blank=True)
        key_expires = models.DateTimeField(default=tomorrow)

        def __str__(self):
            full_name = '%s %s' %(self.first_name,self.last_name)
            return full_name

        class Meta:
            verbose_name=u'User profile'
            verbose_name_plural=u'User profiles'

        models.OneToOneField(CustomUser, related_name='Userprofile')

    class UserAddress(models.Model):
        address_contact=models.CharField(max_length=300,blank=False)
        address_line1=models.CharField(max_length=300,blank=False)
        address_line2=models.CharField(max_length=300,blank=True)
        land_mark=models.CharField(max_length=100,blank=False)
        city=models.CharField(max_length=140,blank=False)
        state=models.CharField(max_length=100,blank=False)
        pin_code = models.BigIntegerField(blank=False)
        mobile_no=models.CharField(max_length=13,blank=True)
        last_shipped_flag=models.BooleanField(default=False)
        is_active_flag=models.BooleanField(default=True)
        is_primary=models.BooleanField(default=False)
        creation_date=models.DateTimeField(auto_now_add=True,editable=False,blank=False,null=True)
        updation_date=models.DateTimeField(auto_now=True,editable=False,blank=False,null=True)
        email=models.ForeignKey(UserProfile)


        def __str__(self):
            return self.address_contact


        class Meta:
            verbose_name=u'User Address'
            verbose_name_plural=u'User Addresses'

this is manager.py

from models import CustomUser

class CustomUserAuth(object):

    def authenticate(self, username = None, password =None):
        try:
            user =CustomUser.objects.get(email=username)
            if user.check_password(password):
                return user
        except CustomUser.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            user=CustomUser.objects.get(pk=user_id)
            if user.is_active:
                return user
            return None
        except CustomUser.DoesNotExist:
            return None



I have included following lines in my settings.py file


    AUTH_USER_MODEL = 'user_management.CustomUser'

    AUTHENTICATION_BACKENDS = ('user_management.manager.CustomUserAuth',)

now for troubleshooting I ran python manage.py shell and import the form

CustomUserCreationForm and passed the data to it and tried to save it. I got the same error

error trace back

 

    Traceback (most recent call last):
      File "", line 1, in 
      File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/forms.py", line 110, in save
        user = super(UserCreationForm, self).save(commit=False)
      File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 459, in save
        if self.errors:
      File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py", line 175, in errors
        self.full_clean()
      File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py", line 386, in full_clean
        self._post_clean()
      File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 413, in _post_clean
        self.instance.full_clean(exclude=exclude, validate_unique=False)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 1235, in full_clean
        self.clean()
      File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py", line 349, in clean
        self.email = self.__class__.objects.normalize_email(self.email)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 198, in __get__
        cls._meta.swapped,
    AttributeError: Manager isn't available; 'auth.User' has been swapped for 'user_management.CustomUser'

There is much info about the following error. I tried to google but I found one bug in django 1.5 for which ticket is closed. Few smart people said I used get_user_model but those information is incomplete.

Please help

1

2 Answers 2

0

Not sure if this is relevant or not but are you getting the UserProfile correctly in your def get_full_name(self): Here you have:

a=UserProfile.objects.get(email_id=self.id)

however I don't see email_id specified in your Class UserProfile. You have email specified and the class is sub-classed from models.Model which, by default, I would expect to only have an id column not email_id unless you have changed it.

Just a thought.

2
  • I fixed it myself. I changed it get_user_model from CustomUser in forms.py for CustomUserCreationForm Class Meta: model = get_user_model() Oct 20, 2017 at 20:43
  • 1
    @user3698694 if you found an accurate fix, please add it as an answer or remove the question if you don't think it is helpful
    – glotchimo
    Jul 24, 2018 at 22:51
0

In Django, the get_user_model() function is used to retrieve the user model that is currently active in the Django project. This function returns the user model that is specified in the AUTH_USER_MODEL setting.

When working with forms in Django, particularly when creating a custom user creation form, it is important to ensure that you reference the correct user model. By default, Django uses the built-in User model, but in some cases, you may have a custom user model defined in your project.

In your case, you made a change in forms.py for the CustomUserCreationForm class. You updated the model attribute in the Meta class of the form to use get_user_model() instead of explicitly specifying CustomUser.

This change ensures that the form uses the active user model defined in the AUTH_USER_MODEL setting. By using get_user_model(), you make your code more flexible and avoid hard-coding the user model, allowing it to work correctly with the default user model or any custom user model you have defined in your project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.