Reputation: 2123
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.
So the response I'm after is like [ { 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?
Upvotes: 0
Views: 453
Reputation: 306
Add another field to the class Product
which is a JSONField
called extra
. Then, use product.extra = {'images': [image.image.url for image in images]}
in the get_queryset() method.
Upvotes: 1