Python 在 Django rest 框架中序列化查询集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26535055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serialize queryset in Django rest framework
提问by mario595
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_idparameter 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.
它接收一个user_id用于确定用户是否有权访问数据的参数,然后它获取要返回的对象集,但它不起作用。
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.
谢谢。
采纳答案by Alex Lisovoy
To serialize a queryset or list of objects instead of a single object instance, you should pass the many=Trueflag when instantiating the serializer. So in your case try this:
要序列化查询集或对象列表而不是单个对象实例,您应该many=True在实例化序列化程序时传递标志。所以在你的情况下试试这个:
...
venues = profile.venue_set.all()
serializer = VenueSerializer(venues, many=True)
...

