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.