User is not logging in admin panel

32 views
Skip to first unread message

sagar ninave

unread,
Dec 16, 2019, 11:38:04 AM12/16/19
to Django users
Hello Everyone,

i am getting some issue please help me.

i have created custom user model in account app by extending auth user model. also i have creates register api to register user. i am able to create superuser from command prompt and api as well. and both user are storing in single User model along with when i am hitting get request and i got response all user which register from command prompt and api. problem is that User is able to logging in admin panel which is registered by command line by command python manage.py createsuperuser but user is not able tologging in admin panel user which is created by register api. when i am seeing data of both user created by cmd and created by register api in admin panel that CMD created user's password is encrypted and user created by register api is plain text. may be at the time of creating superuser password is saving in encrypted form and user registered by api his password is saving in plain text form so that it may not authenticating user to redirect on admin panel. so what is happening exactly please clear this doubt and help to get out from this situation

Tafadzwa Marshal

unread,
Dec 16, 2019, 12:24:01 PM12/16/19
to django...@googlegroups.com
hello, your problem is easy to solve. The 'createsuperuser' command creates a user with the 'staff' attribute set to true. When you create a user through
the model thats inheriting from the User model, the default for the 'staff' attribute is set to false. The solution is to make sure you override the save method
and set the 'staff' attribute to true. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a962e5fd-c41d-465c-a179-14f84ace9551%40googlegroups.com.

Suraj Thapa FC

unread,
Dec 16, 2019, 2:17:44 PM12/16/19
to django...@googlegroups.com

sagar ninave

unread,
Dec 16, 2019, 3:53:29 PM12/16/19
to django...@googlegroups.com
models.py
from django.db import models
import uuid
from django.contrib.auth.models import (BaseUserManager, AbstractBaseUser)
from django.contrib.postgres.fields import ArrayField

class UserManager(BaseUserManager):
    def create_user(self, email, password=None, **kwargs):

        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(email=self.normalize_email(email), **kwargs)

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password=None, **extra_fields):

        user = self.create_user(email, password=password, **extra_fields)
        user.is_admin = True
        user.save(using=self._db)
        return user


class User(AbstractBaseUser):

id = models.AutoField(primary_key=True, unique = True)
# id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
first_name = models.CharField(max_length = 50, blank = False)
last_name = models.CharField(max_length = 50, blank = False)
email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
# password = models.CharField(verbose_name='password', max_length=50)
phone = models.CharField(max_length = 10, blank = False, null = False)
address = models.CharField(max_length = 200, blank = False)
gender = models.CharField(max_length = 50, blank = False)
role = models.CharField(max_length = 50, blank = False)
date_of_birth = models.DateField(null = True, blank = True)
facebook = models.CharField(max_length = 50, blank = True)
twitter = models.CharField(max_length = 50, blank = True)
instagram = models.CharField(max_length = 50, blank = True)
linkedin = models.CharField(max_length = 50, blank = True)
email_verified = models.BooleanField(null = True, default = False)
is_deleted = models.BooleanField(default=False)
deleted_time = models.DateTimeField(auto_now=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
last_login = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True, db_index=True)
modified = models.DateTimeField(auto_now=True)

objects = UserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']

def __str__(self):
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

@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin


Serializers.py


from rest_framework import serializers
from account.models import User
from rest_framework.authtoken.models import Token


class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('__all__')



Views.py

from django.shortcuts import render
# from django.contrib.auth import authenticate
# from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from account.models import User
from account.serializers import UserSerializer
from rest_framework.permissions import AllowAny
from rest_framework import serializers
# import requests


class RegisterUser(APIView):

class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email','password')

def post(self, request): #################################### create user post

first_name = request.data.get("first_name")
last_name = request.data.get("last_name")
email = request.data.get("email")
password = request.data.get("password")
is_admin = request.data.get("is_admin")

user = User()
user.first_name = first_name
user.last_name = last_name
user.email = email
user.password = password
user.is_admin = is_admin
user.save()
return Response({"status": True, "user": UserSerializer(user).data})

def get(self, request, *args, **kwargs):   #################################### get all user
users = User.objects.all()
return Response({"status": True, "users": UserSerializer(users, many = True).data})



--

dumbaclassics

unread,
Dec 16, 2019, 11:18:58 PM12/16/19
to Tafadzwa Marshal, django...@googlegroups.com
I once encountered the same problem with a Custom User Model

The solution that worked for me was putting under the is_staff = True 

It wouldnt log in even afte that then I added the following methods under my User

def has_perm(self, perm, obj=None):
   return True


def has_module_perms(self, app_label):
   return True


and these three decorator functions

@property
def is_admin(self):
  return self.admin


@property
def is_staff(self):
    return self.staff


@property
def is_active(self):
    return self.active



Thus worked for me and you can always try out and see


Thanks



Sent from my Samsung Galaxy smartphone.

sagar ninave

unread,
Dec 17, 2019, 4:27:28 AM12/17/19
to django...@googlegroups.com
thank you. i will try it

Reply all
Reply to author
Forward
0 new messages