Home > Back-end >  How to create a serializer with multiple models, that are connected to each other?
How to create a serializer with multiple models, that are connected to each other?

Time:04-24

So I have UserAccount model, which has this fields:

class UserAccount(AbstractBaseUser, PermissionsMixin):
    
    email = models.EmailField(max_length=255, unique=True)
    iin = models.CharField(max_length=255, unique=True)
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    father_name = models.CharField(max_length=255)
    phone_number = models.CharField(max_length=255)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_doctor = models.BooleanField(default=False)

    objects = UserAccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['iin', 'first_name', 'last_name','father_name', 'phone_number']

    def get_full_name(self):
        return self.first_name   ' '   self.last_name   ' '   self.father_name

    def __str__(self):
        return self.email

And also this class that has user as a foreign key:

class Patient(models.Model):
    user = models.ForeignKey(UserAccount, on_delete=models.CASCADE)
    district = models.CharField(max_length=255)

    def __str__(self):
        return self.user.email

So my question is how to create a serializer for the registration? How am I supposed to create a UserAccount and Patient which has UserAccount as a foreign key together??? PLS HELP This is my diploma work

THIS DOES NOT WORK

class RegistrationSerializer(serializers.ModelSerializer):

    class PatientCreateSerializer(serializers.ModelSerializer):
        
        class Meta:
            model = Patient
            exclude = ['user']
        
    patient = PatientCreateSerializer()

    class Meta:
        model = UserAccount
        fields = '__all__'

    def create(self, validated_data):
        patient_data = validated_data.pop('patient')
        user_instance = UserAccount.objects.create(**validated_data)
        Patient.objects.create(user=user_instance,**patient_data)

        return user_instance

Also, how am I supposed to send data in POSTMAN? LIKE THIS?

{
    "patient": {
        "district": "2323232",
        "user": [{
            "email": "[email protected]",
            "iin": "02020202002",
            "first_name": "Some",
            "last_name": "Somee",
            "father_name": "Someuly",
            "phone_number": " 72783928932",
            "password": "password",
            "re_password": "password"
        }]
    }
}

CodePudding user response:

All OK, but PatientCreateSerializer shouldn't place inside

CodePudding user response:

For example

class PatientCreateSerializer(serializers.ModelSerializer):
    
    class Meta:
        model = Patient
        exclude = ['user']

class RegistrationSerializer(serializers.ModelSerializer):   
    patient = PatientCreateSerializer()

    class Meta:
        model = UserAccount
        fields = '__all__'

    def create(self, validated_data):
        patient_data = validated_data.pop('patient')
        user_instance = UserAccount.objects.create(**validated_data)
        Patient.objects.create(user=user_instance,**patient_data)

        return user_instance

And add in patient.user foreign key params related_name=patiend. If will be errors like "patient in not fields" - description fields by hand

  • Related