I am new to python and Django, this is my first project. I followed a tutorial which returned a list of objects. I want to return json instead.
I have tried JsonResponse, json.dump but I don't think im implementing these right
class ListVenuesView(generics.ListAPIView):
serializer_class = VenueSerialiser
def get_queryset(self):
queryset = (Venue.objects.all())
location = self.request.query_params.get('location', None)
latitude = location.split('S')[0]
longitude = location.split('S')[1]
venue_gaps = {}
for venue in queryset.iterator():
locationArray = [y.strip() for y in venue.postcode.split(',')]
distance = gmaps.distance_matrix([str(latitude) + " " + str(longitude)], [str(locationArray[0]) + " " + str(locationArray[1])], mode='driving')['rows'][0]['elements'][0]
m = distance["distance"]["value"]
venue_gaps[m] = model_to_dict(venue)
sorted_venues = dict(sorted(venue_gaps.items()))
#print(sorted_venues)
jsonResponse = json.dumps(venue_gaps, sort_keys=True)
print(jsonResponse)
return JsonResponse({'data':jsonResponse}, safe=False)
This currently throws
Got AttributeError when attempting to get a value for field `name` on serializer `VenueSerialiser`.
If I replace the return line with
return Venue.objects.all()
I get a 200 but I need it in json
class VenueSerialiser(serializers.ModelSerializer):
class Meta:
model = Venue
fields = ('name', 'location', 'capacity', 'photo_url', 'accomodation', 'cost', 'description', 'postcode', 'email', 'website')
ListAPIView) which already returns JSON anyway. Theget_queryset()method is supposed to return a queryset (or a list).nameon serializerVenueSerialiser. The serializer field might be named incorrectly and not match any attribute or key on thebytesinstance. Original exception text was: 'bytes' object has no attribute 'name'."