I've the following get_or_create method.
class LocationView(views.APIView):
def get_or_create(self, request):
try:
location = Location.objects.get(country=request.data.get("country"), city=request.data.get("city"))
print(location)
return Response(location, status=status.HTTP_200_OK)
except Location.DoesNotExist:
serializer = LocationSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request):
return self.get_or_create(request)
def post(self, request):
return self.get_or_create(request)
This works fine for creating a new location, However if the location exists, I get the following error,
TypeError: Object of type 'Location' is not JSON serializable
[16/Mar/2018 10:10:08] "POST /api/v1/bouncer/location/ HTTP/1.1" 500 96971
This is my model serializer,
class LocationSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(read_only=True)
class Meta:
model = models.Location
fields = ('id', 'country', 'city', 'longitude', 'latitude')
What am I doing wrong here