[英]Django (DRF) - get_queryset - Trying to return "images" with model "Product" as an extra field
I'm trying to make a queryset which returns all Products.我正在尝试创建一个返回所有产品的查询集。 Every Product has one or more ProductImages and I want to add them as an extra field "images" for every product.每个产品都有一个或多个 ProductImages,我想将它们添加为每个产品的额外字段“图像”。
So the response I'm after is like [ { id: 1, name: "prod1", images: images } ]
所以我想要的回应就像[ { id: 1, name: "prod1", images: images } ]
I've tried the following implementations:我尝试了以下实现:
Models楷模
class Product(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
price = models.FloatField()
class ProductImage(models.Model):
name = models.CharField(max_length=255)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
My idea implemented in the simplest way我的想法以最简单的方式实现
class ProductViewSet(viewsets.ModelViewSet):
serializer_class = ProductSerializer
queryset = Product.objects.all()
def get_queryset(self):
queryset = Product.objects.all()
product_list = []
# iterate over all products
for product in queryset:
images = ProductImage.objects.filter(product=q.id)
product['images'] = images
product_list.append(product)
return product_list
gives error给出错误
"TypeError: 'Product' object does not support item assignment"
New idea, make a new dict and add elements of product and add "images" as an extra field.新想法,制作新的字典并添加产品元素并将“图像”添加为额外字段。
class ProductViewSet(viewsets.ModelViewSet):
serializer_class = ProductSerializer
queryset = Product.objects.all()
def get_queryset(self):
queryset = Product.objects.all()
product_list = []
# iterate over all products
for q in queryset:
images = ProductImage.objects.filter(product=q.id)
product = {"id": q.id, "name": q.name, "description": q.description, "price": q.price, "images": images}
product_list.append(product)
return product_list
gives error给出错误
Internal Server Error: /api/product/
Traceback (most recent call last):
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\mixins.py", line 46, in list
return Response(serializer.data)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\serializers.py", line 768, in data
ret = super().data
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\serializers.py", line 253, in data
self._data = self.to_representation(self.instance)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\serializers.py", line 686, in to_representation
return [
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\serializers.py", line 687, in <listcomp>
self.child.to_representation(item) for item in iterable
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\serializers.py", line 509, in to_representation
attribute = field.get_attribute(instance)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\relations.py", line 538, in get_attribute
relationship = get_attribute(instance, self.source_attrs)
File "C:\Users\Simon\.virtualenvs\django_backend-ZmgaAA1F\lib\site-packages\rest_framework\fields.py", line 95, in get_attribute
instance = instance[attr]
KeyError: 'categories'
I've been messing around with this for a while, could anyone point me in the right direction on how to do this?我已经搞砸了一段时间,有人能指出我如何做到这一点的正确方向吗?
Add another field to the class Product
which is a JSONField
called extra
.向class Product
添加另一个字段,这是一个名为extra
的JSONField
。 Then, use product.extra = {'images': [image.image.url for image in images]}
in the get_queryset() method.然后,在 get_queryset() 方法中使用product.extra = {'images': [image.image.url for image in images]}
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.