My API has an endpoint where employees are registered. These are related to the table of users and departments, I do not understand how to obtain and manipulate the information sent from my frontend.
Relationship:
Departament 1-1 CustomUser 1-1 Employees
That’s wrong?
# Employee Logic
class EmployeeList(APIView):
def get(self, request, format=None):
employees = Employee.objects.all()[:99]
serialized_users = EmployeeSerializer(employees, many=True)
return Response(serialized_users.data, status=status.HTTP_200_OK)
class EmployerRegister(APIView):
def post(self, request):
# save all fields all tables
serialized_user = EmployeeSerializer(data=request.data)
serialized_employee = EmployeeSerializer(data=request.data)
serialized_departament = DepartamentSerializer(data=request.data)
if serialized_user.is_valid() and serialized_employee.is_valid(
) and serialized_departament.is_valid():
# this is wrong?
serialized_user.save()
serialized_employee(serialized_user)
serialized_employee.save()
serialized_departament(serialized_user)
serialized_departament.save()
return Response(serialized_user.data,
status=status.HTTP_201_CREATED)
return Response(serialized_user.errors)
Models
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
class CustomUserManager(BaseUserManager):
def create_user(self, email, password=None):
if not email:
raise ValueError("Users must have an email address")
user = self.model(email=self.normalize_email(email), )
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password=None):
user = self.create_user(
email,
password=password,
)
user.is_admin = True
user.save(using=self._db)
return user
class CustomUser(AbstractBaseUser):
email = models.EmailField(
verbose_name="email address",
max_length=255,
unique=True,
)
is_active = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
email_confirmed = models.BooleanField(default=False)
last_modified = models.DateTimeField(auto_now=True)
password = models.CharField(max_length=128)
last_login = models.DateTimeField(blank=True, null=True)
date_joined = models.DateTimeField(auto_now_add=True)
objects = CustomUserManager()
USERNAME_FIELD = "email"
REQUIRED_FIELDS = []
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
# Employee Model
class Employee(models.Model):
#Char
name_one = models.CharField(max_length=30,
default='',
null=True,
blank=True)
name_two = models.CharField(max_length=30,
default='',
null=True,
blank=True)
surname_one = models.CharField(max_length=30,
default='',
null=True,
blank=True)
surname_two = models.CharField(max_length=30,
default='',
null=True,
blank=True)
job_title = models.CharField(max_length=100,
default='',
null=True,
blank=True)
phone = models.CharField(max_length=30, default='', null=True, blank=True)
home_phone = models.CharField(max_length=30,
default='',
null=True,
blank=True)
phote = models.CharField(max_length=50, default='', null=True, blank=True)
phone_path = models.CharField(max_length=50,
default='',
null=True,
blank=True)
# Text
notes = models.TextField(blank=True)
# Number
extension = models.PositiveSmallIntegerField(default=0,
null=True,
blank=True)
# Binary
gender = models.BinaryField(null=True, blank=True)
# Date - Time
birth_date = models.DateField(null=True, blank=True)
hire_date = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
# Navigation property
user = models.OneToOneField(
CustomUser,
primary_key=True,
on_delete=models.CASCADE,
)
class Meta:
db_table = "Employee.Data"
ordering = ["-name_one"]
verbose_name = "Employee"
verbose_name_plural = "Employees"
# Departament Model
class Departament(models.Model):
name = models.CharField(max_length=100)
# Navigation property
user = models.OneToOneField(
CustomUser,
primary_key=True,
on_delete=models.CASCADE,
)
class Meta:
db_table = "Employee.Departament"
ordering = ["-name"]
verbose_name = "Departament"
verbose_name_plural = "Departaments"
Serializer
class CustomUserSerializer(serializers.ModelSerializer):
# Serializers relation fields
employee = EmployeeSerializer()
departament = DepartamentSerializer()
class Meta:
model = CustomUser
fields = "__all__"
extra_kwargs = {"password": {"write_only": True}}
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
fields = "__all__"
class DepartamentSerializer(serializers.ModelSerializer):
class Meta:
model = Departament
fields = "__all__"