Django Rest 框架:将查询集传递给 Response()
Django Rest framework : passing queryset to Response()
我在 application.In 视图中使用 django rest 框架,为了获取查询,我正在获取查询集形式的模型列表并尝试传递给我的 Response()
organizationViews.py
def get(self,request):
user = self.request.user
result = OrganizationModel.object.all()
serializer = OrganizationSerializer("json", list(result), many=True)
return Response(serializer, status=status.HTTP_200_OK)
organizationModel.py
class OrganizationManager(models.Manager):
def create_organization(self, _name):
org = self.model( name = _name)
org.save(using=self._db)
return org
class Organization(BaseModel):
name = models.CharField(_('organization'), max_length=100, blank=True,unique=True)
def as_json(self):
return dict(
id=self.id,
org_name=self.name)
object = OrganizationManager()
class Meta(BaseModel.Meta):
db_table = 'organizations'
verbose_name = _('organization')
verbose_name_plural = _('organizations')
def __str__(self):
return self.name
我收到以下错误:
Traceback (most recent call last):
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/core/handlers/base.py", line 174, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/core/handlers/base.py", line 172, in get_response
response = response.render()
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/template/response.py", line 160, in render
self.content = self.rendered_content
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/rest_framework/response.py", line 71, in rendered_content
ret = renderer.render(self.data, media_type, context)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/rest_framework/renderers.py", line 104, in render
separators=separators
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/rest_framework/utils/encoders.py", line 64, in default
return super(JSONEncoder, self).default(obj)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 180, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: OrganizationSerializer('json', [<Organization: zeotap>, <Organization: movial>, <Organization: Redbull>, <Organization: mogey>, <Organization: vizury>], many=True):
id = IntegerField()
name = CharField() is not JSON serializable
如果我在 get call 中执行以下操作:
serialized_q= json.dumps(list(queryset), cls=DjangoJSONEncoder)
return Response(serialized_q, status=status.HTTP_200_OK)
我没有收到错误,但结果如下所示:
"[{\"id\": 1, \"soft_delete\": false, \"created_at\": \"2016-04-14T13:35:21.636Z\", \"name\": \"org1\", \"updated_at\": \"2016-04-14T13:35:21.636Z\"},
{\"id\": 4, \"soft_delete\": false, \"created_at\": \"2016-04-14T13:37:02.230Z\", \"name\": \"org2\", \"updated_at\": \"2016-04-14T13:37:02.230Z\"}]"
我应该如何将查询集传递给 Response 方法?
我认为你只能使用序列化器来序列化单个模型。所以而不是:
serializer = OrganizationSerializer("json", list(result), many=True)
尝试:
data = [
OrganizationSerializer(model).data
for model in result
]
然后:
return Response(data)
参见:http://www.django-rest-framework.org/api-guide/serializers/#serializing-objects
您并不是真的想覆盖 get 方法,您最好覆盖 get_queryset;
class ViewSet(ModelViewSet):
queryset = Model.objects.all()
serializer_class = ModelSerializer
def get_queryset(self): #this method is called inside of get
queryset = self.queryset.filter(doyourfiltering=True)
return queryset
你可以在这里直接 return 一个查询集,Django 会为你序列化它。您将可以通过 self.request.
访问该请求
我在 application.In 视图中使用 django rest 框架,为了获取查询,我正在获取查询集形式的模型列表并尝试传递给我的 Response()
organizationViews.py
def get(self,request):
user = self.request.user
result = OrganizationModel.object.all()
serializer = OrganizationSerializer("json", list(result), many=True)
return Response(serializer, status=status.HTTP_200_OK)
organizationModel.py
class OrganizationManager(models.Manager):
def create_organization(self, _name):
org = self.model( name = _name)
org.save(using=self._db)
return org
class Organization(BaseModel):
name = models.CharField(_('organization'), max_length=100, blank=True,unique=True)
def as_json(self):
return dict(
id=self.id,
org_name=self.name)
object = OrganizationManager()
class Meta(BaseModel.Meta):
db_table = 'organizations'
verbose_name = _('organization')
verbose_name_plural = _('organizations')
def __str__(self):
return self.name
我收到以下错误:
Traceback (most recent call last):
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/core/handlers/base.py", line 174, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/core/handlers/base.py", line 172, in get_response
response = response.render()
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/template/response.py", line 160, in render
self.content = self.rendered_content
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/rest_framework/response.py", line 71, in rendered_content
ret = renderer.render(self.data, media_type, context)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/rest_framework/renderers.py", line 104, in render
separators=separators
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/rest_framework/utils/encoders.py", line 64, in default
return super(JSONEncoder, self).default(obj)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 180, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: OrganizationSerializer('json', [<Organization: zeotap>, <Organization: movial>, <Organization: Redbull>, <Organization: mogey>, <Organization: vizury>], many=True):
id = IntegerField()
name = CharField() is not JSON serializable
如果我在 get call 中执行以下操作:
serialized_q= json.dumps(list(queryset), cls=DjangoJSONEncoder)
return Response(serialized_q, status=status.HTTP_200_OK)
我没有收到错误,但结果如下所示:
"[{\"id\": 1, \"soft_delete\": false, \"created_at\": \"2016-04-14T13:35:21.636Z\", \"name\": \"org1\", \"updated_at\": \"2016-04-14T13:35:21.636Z\"},
{\"id\": 4, \"soft_delete\": false, \"created_at\": \"2016-04-14T13:37:02.230Z\", \"name\": \"org2\", \"updated_at\": \"2016-04-14T13:37:02.230Z\"}]"
我应该如何将查询集传递给 Response 方法?
我认为你只能使用序列化器来序列化单个模型。所以而不是:
serializer = OrganizationSerializer("json", list(result), many=True)
尝试:
data = [
OrganizationSerializer(model).data
for model in result
]
然后:
return Response(data)
参见:http://www.django-rest-framework.org/api-guide/serializers/#serializing-objects
您并不是真的想覆盖 get 方法,您最好覆盖 get_queryset;
class ViewSet(ModelViewSet):
queryset = Model.objects.all()
serializer_class = ModelSerializer
def get_queryset(self): #this method is called inside of get
queryset = self.queryset.filter(doyourfiltering=True)
return queryset
你可以在这里直接 return 一个查询集,Django 会为你序列化它。您将可以通过 self.request.
访问该请求