1

I currently have the following structure in my DRF api.

models.py

class Location(models.Model):
    name = models.CharField(max_length=64, unique=True)
    district = models.CharField(max_length=64, blank=True)
    division = models.CharField(max_length=64, blank=True)
    latitude = models.DecimalField(max_digits=9, decimal_places=3, blank=True)
    longitude = models.DecimalField(max_digits=9, decimal_places=3, blank=True)

class Event(models.Model):
    title = models.CharField(max_length=64)
    location = models.ForeignKey(Location, on_delete=models.CASCADE, blank=False, null=False)
    type = models.CharField(max_length=64, blank=True)
    max_quota = models.IntegerField(blank=True)
    min_cost = models.IntegerField(blank=True)
    duration_start = models.CharField(max_length=64, blank=True)
    duration_end = models.CharField(max_length=64, blank=True)

serializers.py

class LocationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Location
        fields = ['name', 'district', 'division', 'latitude', 'longitude', ]

class ExperienceCreateSerializer(serializers.ModelSerializer):
    location = LocationSerializer(many=False)
    #location_id = serializers.PrimaryKeyRelatedField(read_only=True)

    class Meta:
        model = Experience
        fields = ['title', 'type', 'max_quota', 'min_cost', 'duration_start', 'duration_end', 'location', ]

views.py

class ExperienceCreate(generics.CreateAPIView):
    queryset = Experience.objects.all()
    serializer_class = ExperienceCreateSerializer

My get requests work fine, but when I want to POST to the Event models, then I am always getting some sort of error. I have tried quite a few things including overriding the create method, also tried to use the primarykeyfield. The problem is with the location foreign key reference within the Event model and serializers. I did try out a couple of things other than this but the only solution that made sense was to override the create() method. But, nothing whatsoever has worked. I don't understand exactly where I am going wrong.

Aneesh R S
  • 3,807
  • 4
  • 23
  • 35
Mehran
  • 1,264
  • 10
  • 27
  • show you version of `create()` – Brown Bear Aug 23 '17 at 11:00
  • I've asked the same question before. I think you will find this helpful: https://stackoverflow.com/questions/41312558/django-rest-framework-post-nested-objects – wencakisa Aug 23 '17 at 11:10
  • `code def create(self, validated_data): ` `code loc_data = validated_data.pop('location') ` `code exp = Experience.objects.create(**validated_data) ` `code Experience.objects.create(location=loc_data, **track_data) ` `code return exp ` – Mehran Aug 23 '17 at 11:57

1 Answers1

0

Well, I found a workaround for this. If the location object is already created, then what we need to do is pass the location as PrimaryKeyRelatedField in the serializer. N.B. the write_only needs to be made true

location = serializers.PrimaryKeyRelatedField(queryset=Location.objects.all(), write_only=True)
Aneesh R S
  • 3,807
  • 4
  • 23
  • 35
Mehran
  • 1,264
  • 10
  • 27