0

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')
10
  • 2
    why are you returning a JsonResponse in the get_queryset() method???? And it seems you're not using plain Django but django-rest-framework, (ListAPIView) which already returns JSON anyway. The get_queryset() method is supposed to return a queryset (or a list). Commented Aug 22, 2019 at 15:12
  • oh okay, sorry I was just following a tutorial. Do I need to create a separate function to return json? Commented Aug 22, 2019 at 15:15
  • your view is already returning json. You don't need to do anything. If you type the url to your view in a browser you'll see that the response is json. Commented Aug 22, 2019 at 15:16
  • when I run the code above I get the error - "AttributeError at /api/v1/venues/ Got AttributeError when attempting to get a value for field name on serializer VenueSerialiser. The serializer field might be named incorrectly and not match any attribute or key on the bytes instance. Original exception text was: 'bytes' object has no attribute 'name'." Commented Aug 22, 2019 at 15:17
  • I've added my serialiser class too Commented Aug 22, 2019 at 15:18

1 Answer 1

1

thanks to Daniel Roseman and dirkgroten I changed get_queryset to get and the json was returned. Thanks for the help :)

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.