*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})
On Mon, Dec 16, 2019 at 6:15 AM Suraj Thapa FC <[email protected]>
wrote:
> Where's the code
>
> On Mon, 16 Dec 2019, 5:53 pm Tafadzwa Marshal, <[email protected]>
> wrote:
>
>> 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.
>>
>> On Mon, Dec 16, 2019 at 3:38 AM sagar ninave <[email protected]>
>> wrote:
>>
>>> 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
>>>
>>> --
>>> 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 [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/a962e5fd-c41d-465c-a179-14f84ace9551%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/a962e5fd-c41d-465c-a179-14f84ace9551%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
>> 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 [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAFgO-6hUKDCcHqt9vZP645rAksuKjNhuPRyG3JPv1TQTQL948A%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAFgO-6hUKDCcHqt9vZP645rAksuKjNhuPRyG3JPv1TQTQL948A%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAPjsHcHx6HQ44E0Ufox2%3D4ZQJMYOub6Kro-rn0%3DbhQi%2BoCCrPg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAPjsHcHx6HQ44E0Ufox2%3D4ZQJMYOub6Kro-rn0%3DbhQi%2BoCCrPg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
--
<https://about.me/sagarninave?promo=email_sig&utm_source=product&utm_medium=email_sig&utm_campaign=gmail_api&utm_content=thumb>
sagar ninave
about.me/sagarninave
<https://about.me/sagarninave?promo=email_sig&utm_source=product&utm_medium=email_sig&utm_campaign=gmail_api&utm_content=thumb>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAA6pdZ-s6EPbn9UO6oj5UaG3RYZKFg3bbzmfN3HQ55praP4C5Q%40mail.gmail.com.