0

My data input is in the form of list of 'n' number of dicts

"contact_person":[
               {
                   "contactperson_salutation[0]":"sddd",
                   "contactperson_first_name[0]":"santoorr",
                   "contactperson_last_name[0]":"",
                   "contactperson_email[0]":"gfgh",
                   "contactperson_mobile_number[0]":"",
                   "contactperson_work_phone_number[0]":"jio"
               },
               {
                   "contactperson_salutation[1]":"dfsf",
                   "contactperson_first_name[1]":"lux",
                   "contactperson_last_name[1]":"",
                   "contactperson_email[1]":"",
                   "contactperson_mobile_number[1]":"",
                   "contactperson_work_phone_number[1]":"9048"
               }, .............]

My model is like this:

class ContactPerson(models.Model):

   client = models.ForeignKey(Client, on_delete=models.CASCADE)
   contactperson_salutation = models.CharField(max_length=4, choices=SALUTATIONS)
   contactperson_first_name = models.CharField(max_length=128)
   contactperson_last_name = models.CharField(max_length=128, blank=True)
   contactperson_email = models.EmailField(blank=True, null=True)
   contactperson_mobile_number = models.CharField(max_length=20, blank=True)
   contactperson_work_phone_number = models.CharField(max_length=20, blank=True)

How to write serializer when the fields names are changing for every dict in the input list..

And if errors occurs the Error Response should be in this format:

            [
                {
                    "contactperson_email[0]":"Invalid Email",
                    "contactperson_mobile_number[0]":"Invalid mobile phone",
                    "contactperson_work_phone_number[0]":"Invalid workphone number"
                },
                {
                    "contactperson_mobile_number[1]":"Invalid mobile phone",
                    "contactperson_work_phone_number[1]":"Invalid workphone number"
                }
             ]

beginners
  • 305
  • 3
  • 16

2 Answers2

0

You would probably benefit from parsing the "contactperson_salutation[0]"-like strings to build a list contactperson_salutation with all the ocurrences.

Same thing for each of the other fields.

Timothé Delion
  • 1,310
  • 12
  • 17
0

try overwriting the to_internal_value method of the Serializer to achieve this.

But the error would still be not in the format you require. The error message will contain your model keys, not the keys which are suffixed with [x].

class ContactPersonSerializer(serializers.ModelSerializer):

    class Meta:
        model = ContactPerson
        fields = [
        "contactperson_salutation",
        "contactperson_first_name",
        "contactperson_last_name",
        "contactperson_email",
        "contactperson_mobile_number",
        "contactperson_work_phone_number",
    ]

    def to_internal_value(self, data):
        if hasattr(data, "_mutable"):
            data._mutable = True

        data = {key[:-3]: value for key, value in data.items()}

        if hasattr(data, "_mutable"):
            data._mutable = False

        return super().to_internal_value(data)
Vishal Singh
  • 6,014
  • 2
  • 17
  • 33