Dec-05-2021, 01:01 AM
How to work with 2 or more related tables in a Post request?
I have 3 tables:
I have another view to record
The problem is that I don't know how to manipulate 3 tables in a single request
In this view the standard user can register in a single request the data of these 3 tables
EndPoints
domainapp.com/dashboard/users/
domainapp.com/dashboard/employees/
src.apps.account.models.CustomUser
domainapp.com/dashboard/users/
This working perfect!
src.apps.account.view
domainapp.com/dashboard/employees/
This problem is here
src.apps.employee.view
what I need to do here?
Please help me!
I have 3 tables:
- Employee (access for Standart Users)
- CustomUser (access for Only Adminns)
- Departament (access for Standart Users)
users
(only admins can enter)I have another view to record
Employees
(adminins and standard users can enter)The problem is that I don't know how to manipulate 3 tables in a single request
In this view the standard user can register in a single request the data of these 3 tables
EndPoints
domainapp.com/dashboard/users/
domainapp.com/dashboard/employees/
src.apps.account.models.CustomUser
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_adminsrc.apps.employee.models.Employee
# 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"src.apps.employee.models.Departaments
# 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 Employe Departament CustomUser
from rest_framework import serializers from .models import Employee, Departament, CustomUser class CustomUserSerializer(serializers.ModelSerializer): employee = EmployeeSerializer() # HERE Serializer Relational ! 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__"EndPoint
domainapp.com/dashboard/users/
This working perfect!
src.apps.account.view
class UserList(APIView): def get(self, request, format=None): users = CustomUser.objects.all()[:99] serialized_users = CustomUserSerializer(users, many=True) return Response(serialized_users.data, status=status.HTTP_200_OK) class UserRegister(APIView): def post(self, request): # user_info = request.data serialized_user = CustomUserSerializer(data=request.data) if serialized_user.is_valid(): new_user = serialized_user.save() new_user.set_password(new_user.password) new_user.save() return Response(serialized_user.data, status=status.HTTP_201_CREATED) return Response(serialized_user.errors)EndPoint
domainapp.com/dashboard/employees/
This problem is here
src.apps.employee.view
what I need to do here?
Please help me!
from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView from .models import Employee from .serializer import EmployeeSerializer, DepartamentSerializer # 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): 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(): 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)