0

I am trying to post array of objects in JSON data using Postman , but unable to create the object,how will i achieve it?Here is my code

```
models.py
```

class Classes(models.Model):
    name = models.CharField(max_length=250, blank=True, null=True)
    description = models.CharField(max_length=1000, blank=True, null=True)

class ClassDaySlot(models.Model):
    class_id = models.ForeignKey(Classes, on_delete=models.CASCADE, blank=True, null=True,related_name='dayslot_classes')

    day = models.CharField(max_length=50, blank=True, null=True)



class ClassTimeSlot(models.Model):
    day_slot_id = models.ForeignKey(ClassDaySlot, on_delete=models.CASCADE, blank=True, null=True,related_name='my_class_timeslot')
    start_slot_time = models.TimeField(blank=True, null=True)
    end_slot_time = models.TimeField(blank=True, null=True)
```
serializers.py
```

class ClassUpdateSerializer(serializers.ModelSerializer):
    class_day = serializers.ListField()
    class_time = serializers.ListField()

    class Meta:
        model = Classes

    def create(self, validated_data):
        dayslot_validated_data = validated_data.pop('class_day', None)
        timeslot_validated_data = validated_data.pop('class_time', None)
        classes = Classes.objects.create(**validated_data)

        if dayslot_validated_data:
            for day_slot in dayslot_validated_data:
                created_day_class = ClassDaySlot.objects.create(**day_slot, class_id=classes)
                if timelot_validated_data:
                    for time_slot in timeslot_validated_data:
                        ClassTimeSlot.objects.create(**time_slot, day_slot_id=created_day_class)
        return classes

I have to post json data in postman exactly like this:
    {
    "class_day":[{
                "day":"friday",
                "class_time":[
                        {
                        "start_slot_time":"08:00:00+0000",
                        "end_slot_time":"09:00:00+0000"
                        }

                ]           

            }]
    }

I am getting this error: "ClassDaySlot() got an unexpected keyword argument 'class_time'". if i give "class_time" outside "class_day" list, it works but i don't want that, what i want is to post array of "day" and "class_time" objects which i mentioned in the above JSON format.Any idea how to achieve this

1 Answer 1

1

You can just pop class_time before passing it to ClassDaySlot.objects.create.

class ClassUpdateSerializer(serializers.ModelSerializer):
    class_day = serializers.ListField()

    class Meta:
        model = Classes

    def create(self, validated_data):
        dayslot_validated_data = validated_data.pop('class_day', None)
        classes = Classes.objects.create(**validated_data)

        if dayslot_validated_data:
            for day_slot in dayslot_validated_data:
                timeslot_validated_data = day_slot.pop('class_time', None)
                created_day_class = ClassDaySlot.objects.create(**day_slot, class_id=classes)
                if timelot_validated_data:
                    for time_slot in timeslot_validated_data:
                        ClassTimeSlot.objects.create(**time_slot, day_slot_id=created_day_class)
        return classes
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.