Моя пользовательская модель пользователя
from django.db import models
from django.contrib.auth.models import AbstractBaseUser,BaseUserManager
class UserManager(BaseUserManager):
def create_user(self,email, first_name, last_name, role_id, org_id, status, created_by, modified_by, password=None, is_admin=False, is_staff=False):
if not email:
raise ValueError("email id is required")
if not password:
raise ValueError("password must required")
if not first_name:
raise ValueError("first_name is required")
if not last_name:
raise ValueError("last_name is required")
if not role_id:
raise ValueError("role_id is required")
if not org_id:
raise ValueError("org_id is required")
if not status:
raise ValueError("status is required")
if not created_by:
raise ValueError("created_by is required")
if not modified_by:
raise ValueError("modified_by is required")
user_obj=self.model(
email=self.normalize_email(email)
)
user_obj.set_password(password)
user_obj.first_name=first_name
user_obj.last_name=last_name
user_obj.role_id=role_id
user_obj.org_id=org_id
user_obj.status=status
user_obj.created_by=created_by
user_obj.modified_by=modified_by
user_obj.admin=is_admin
user_obj.staff=is_staff
user_obj.save(using=self._db)
return user_obj
def create_staffuser(self,email,first_name, last_name, role_id, org_id, status, created_by, modified_by, password=None):
user=self.create_user(
email,
first_name,
last_name,
role_id,
org_id,
status,
created_by,
modified_by,
password=password,
is_admin=False,
is_staff=True
)
return user
def create_superuser(self,email,first_name, last_name, role_id, org_id, status, created_by, modified_by, password=None):
user=self.create_user(
email,
first_name,
last_name,
role_id,
org_id,
status,
created_by,
modified_by,
password=password,
is_admin=True,
is_staff=True
)
return user
Status = [
('Active', 'Active'),
('Inactive', 'Inactive'),
('Deleted', 'Deleted')
]
class User(AbstractBaseUser):
username = None
last_login = None
email=models.EmailField(max_length=255,unique=True)
admin=models.BooleanField(default=False)
staff=models.BooleanField(default=False)
first_name=models.CharField(max_length=200)
last_name=models.CharField(max_length=200)
role_id = models.ForeignKey('roles_tasks.mst_org_roles', on_delete=models.DO_NOTHING)
org_id = models.ForeignKey('organizations.config_orgs', on_delete=models.DO_NOTHING)
project_id = models.ManyToManyField('projects.mst_projects', blank=True)
status = models.CharField(choices = Status, default = 'Active', max_length = 15)
created_by = models.IntegerField(blank=True, null=True)
created_date = models.DateTimeField(auto_now_add = True, blank=True, null=True)
modified_by = models.IntegerField(blank=True, null=True)
modified_date = models.DateTimeField(auto_now = True, blank=True, null=True)
objects = UserManager()
def get_first_name(self):
return (self.first_name)
def get_last_name(self):
return (self.last_name)
def get_user_id(self):
return self.id
def get_status(self):
return self.status
def has_perm(self,perm,obj=None):
return True
def has_module_perms(self,app_label):
return True
def __str__(self):
return (self.email)
USERNAME_FIELD="email"
REQUIRED_FIELDS=['first_name', 'last_name', 'role_id', 'org_id', 'status', 'created_by', 'modified_by']
@property
def is_active(self):
return self.status
@property
def is_admin(self):
return self.admin
@property
def is_staff(self):
return self.staff
Я использую Django Rest Framework для создания API для этой модели пользователя. Когда я пытаюсь добавить пользователя, используя это нижеуказанное тело.
{
"first_name": "FName",
"last_name": "LName",
"role_id": 1,
"org_id": 1,
"project_id": [1,2],
"created_by": 1,
"modified_by": 1,
"status": "Active",
"email": "emailaddress@gmail.com",
"password": "pass12345"
}
Я получаю вывод, так как
{
"first_name": "FName",
"last_name": "LName",
"role_id": 1,
"org_id": 1,
"created_by": 1,
"modified_by": 1,
"status": "Active",
"email": "emailaddress@gmail.com",
"password": "pass12345"
}
идентификатор проекта не добавляется в базу данных, может кто-нибудь помочь my with Что пошло не так?
Я использую библиотеку Djoser для добавления пользователя
http://127.0.0.1: 8000 / auth / users /
This является конечной точкой в Djoser для создания пользователя.