Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

python - Serialize queryset in Django rest framework

I am trying to serialize a collection of objects. I have define the following view method:

@csrf_exempt
def venue_list(request, user_id):
    """
    Check that the user is requesting his own venues.
    """
    profile = get_profile_for_user_if_match(request.user, user_id)

    if profile is None:
        return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)

    venues = profile.venue_set.all()
    serializer = VenueSerializer(venues)

    return JSONResponse(serializer.data)

It receives a user_id parameter which is used to determine if the user has permissions to access the data, then it gets the set of objects to be returned, but it doesn't work.

It is trying to serialize the set directly, instead of the object inside it, so its throwing this traceback:

Traceback:
File "/Users/mariopersonal/Documents/dev/offers/project/offers/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/mariopersonal/Documents/dev/offers/project/offers/venv/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "/Users/mariopersonal/Documents/dev/offers/project/offers/wsgi/openshift/business/restful/views/venueViews.py" in venue_list
  22.     return JSONResponse(serializer.data)
File "/Users/mariopersonal/Documents/dev/offers/project/offers/venv/lib/python2.7/site-packages/rest_framework/serializers.py" in data
  572.                 self._data = self.to_native(obj)
File "/Users/mariopersonal/Documents/dev/offers/project/offers/venv/lib/python2.7/site-packages/rest_framework/serializers.py" in to_native
  351.             value = field.field_to_native(obj, field_name)
File "/Users/mariopersonal/Documents/dev/offers/project/offers/venv/lib/python2.7/site-packages/rest_framework/fields.py" in field_to_native
  336.         return super(WritableField, self).field_to_native(obj, field_name)
File "/Users/mariopersonal/Documents/dev/offers/project/offers/venv/lib/python2.7/site-packages/rest_framework/fields.py" in field_to_native
  207.             value = get_component(value, component)
File "/Users/mariopersonal/Documents/dev/offers/project/offers/venv/lib/python2.7/site-packages/rest_framework/fields.py" in get_component
  58.         val = getattr(obj, attr_name)

Exception Type: AttributeError at /business/api/venues/1
Exception Value: 'QuerySet' object has no attribute 'name'

How can I make this properly?

Thanks.

question from:https://stackoverflow.com/questions/26535055/serialize-queryset-in-django-rest-framework

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To serialize a queryset or list of objects instead of a single object instance, you should pass the many=True flag when instantiating the serializer. So in your case try this:

...
venues = profile.venue_set.all()
serializer = VenueSerializer(venues, many=True)
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...