django REST framework - limited queryset for nested ModelSerializer? - django

I have a ModelSerializer, but by default it serializes all the objects in my model. I would like to limit this queryset to only the most recent 500 (as opposed to all 50 million). How do I do this?
What I have currently is the following:
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
The reason I don't think I can just specify the queryset in my viewset is that this is in fact the nested portion of another serializer.
models.py
class Container(models.Model):
size = models.CharField(max_length=20)
shape = models.CharField(max_length=20)
class Item(models.Model):
container = models.ForeignKey(Container, related_name='items')
name = models.CharField(max_length=20)
color = models.CharField(max_length=20)
views.py
class ContainerViewSet(viewsets.ModelViewSet):
queryset = Container.objects.all() # only a handful of containers
serializer_class = ContainerSerializer
serializers.py
class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = Item
fields = ('name', 'color')
class ContainerSerializer(serializers.ModelSerializer):
items = ItemSerializer(many=True) # millions of items per container
class Meta:
model = Container
fields = ('size', 'shape', 'items')

In your View Set you may specify the queryset like follows:
from rest_framework import serializers, viewsets
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
class MyModelViewSet(viewsets.ModelViewSet):
queryset = MyModel.objects.all()[:500]
serializer_class = MyModelSerializer
I think what you are looking for is the SerializerMethodField.
So your code would look as follows:
class ContainerSerializer(serializers.ModelSerializer):
items = SerializerMethodField('get_items')
class Meta:
model = Container
fields = ('size', 'shape', 'items')
def get_items(self, container):
items = Item.objects.filter(container=container)[:500] # Whatever your query may be
serializer = ItemSerializer(instance=items, many=True)
return serializer.data
The one catch is that the SerializerMethodField is read only.

You may use source parameter
class Container(models.Model):
...
def get_items(self):
return self.items[:500]
and in serializer
items = ItemSerializer(many=True, source='get_items', )

Related

Is there a way to dynamically specify a queryset for nested relationship (nested serializer class) in django rest framework

Suppose we have two models:
class Chapter(models.Model):
title = models.CharField(max_length=128)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
class Post(models.Model):
title = models.CharField(max_length=128)
body = models.TextField()
is_archived = models.BooleanField(default=False)
chapter = models.ForeignKey(Chapter, on_delete=models.CASCADE)
And default ModelViewSet viewset for Chapter model:
class ChapterViewSet(viewsets.ModelViewSet):
queryset = Chapter.objects.all()
serializer_class = ChapterSerializer
The key thing is that ChapterSerializer performs nested serialization using PostSerializer to provide a post_set key in the response.
class PostSerializer(serializers.HyperlinkedModelSerializer):
detail_url = HyperlinkedIdentityField(view_name='post-detail', read_only=True)
class Meta:
fields = ['id', 'title', 'is_archived', 'detail_url']
model = Post
class ChapterSerializer(serializers.ModelSerializer):
post_set = PostSerializer(read_only=True, many=True)
class Meta:
model = Chapter
fields = ['id', 'title', 'owner', 'post_set']
The question is how I can dynamically specify a queryset for this nested PostSerializer. For example, when user makes GET request I only want to include the posts that are not archived (is_archived field is set to False) if user, who has done a request, is not an owner of a Chapter (request.user != current_chapter.owner). Is there any way to achive it?
You can use prefetch_related to prefetch the results used by a nested serializer, this prefetch can be filtered by using a Prefetch object and this will then filter the nested results
class ChapterViewSet(viewsets.ModelViewSet):
queryset = Chapter.objects.all()
serializer_class = ChapterSerializer
def get_queryset(self):
queryset = super().get_queryset()
return queryset.prefetch_related(
Prefetch('post_set', queryset=Post.objects.filter(is_archived=False))
)
In the get_queryset method you will have to perform this prefetching dynamically, the current request can be accessed via self.request

How to prefetch_related() for GenericForeignKeys?

I have a List that consists of ListItems. These ListItems then point towards either a ParentItem or a ChildItem model via a GenericForeignKey:
# models.py
class List(models.Model):
title = models.CharField()
class ListItem(models.Model):
list = models.ForeignKey(List, related_name="list_items")
order = models.PositiveSmallIntegerField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey("content_type", "object_id")
class ParentItem(models.Model):
title = models.CharField()
class ChildItem(models.Model):
title = models.CharField()
parent = models.ForeignKey(ParentItem, related_name="child")
I want to display a list of all my Lists with their ListItems and respective ItemA/ItemB data using ListSerializer:
# serializers.py
class ParentItemSerializer(serializers.ModelSerializer):
class Meta:
model = ParentItem
fields = ["title"]
class ChildItemSerializer(serializers.ModelSerializer):
parent = ParentItemSerializer()
class Meta:
model = ChildItem
fields = ["title", "parent"]
class ListItemSerializer(serializers.ModelSerializer):
contents = serializers.SerializerMethodField()
class Meta:
model = ListItem
fields = ["contents"]
def get_contents(self, obj):
item = obj.content_object
type = item.__class__.__name__
if type == "ParentItem":
return ParentItemSerializer(item).data
elif type == "ChildItem":
return ChildItemSerializer(item).data
class ListSerializer(serializers.ModelSerializer):
items = serializers.SerializerMethodField()
class Meta:
model = List
fields = ["title", "items"]
def get_items(self, obj):
return ListItemSerializer(obj.list_items, many=True).data
How can I optimize my List queryset to prefetch these GenericForeignKey relationships?
# views.py
class ListViewSet(viewset.ModelViewSet):
queryset = List.objects.all()
serializer_class = ListSerializer
List.objects.all().prefetch_related("list_items") works but the following does not seem to work:
List.objects.all().prefetch_related(
"list_items",
"list_items__content_object",
"list_items__content_object__parent",
)
I've read the documentation on prefetch_related which suggests it should work:
While prefetch_related supports prefetching GenericForeignKey
relationships, the number of queries will depend on the data. Since a
GenericForeignKey can reference data in multiple tables, one query per
table referenced is needed, rather than one query for all the items.
There could be additional queries on the ContentType table if the
relevant rows have not already been fetched.
but I don't know if that's applicable to DRF.
Edit: Some better success when I move some of the prefetching to the serializer:
class ListSerializer(serializers.ModelSerializer):
def get_items(self, obj):
return ListItemSerializer(obj.list_items.all().prefetch_related("content_object"), many=True).data
As per my edit, prefetching works as intended when I move the relevant fields when they're queried in the serializer instead of cramming it all in the view's queryset.

Write operation in Reverse Relationship Django Rest Framework

I have models like this:
class Car(models.Model):
name = models.CharField(max_length=255)
class CarImage(models.Model):
car = models.ForeignKey(Car, related_name='photos')
photo = models.ImageField(upload_to='car/')
For the serializer I have:
class CarImageSerializer(serializer.ModelSerializer):
class Meta:
model = CarImage
class CarSerializer(serializer.ModelSerializer):
photos = CarImageSerializer()
class Meta:
model = Car
fields = ('id', 'name', 'photos',)
When the web interface for CarSerializer loads I get non_field_errors on the photos field by default. Is this kind of thing supported by DRF? If not what's the best way to do this?
P.S I am using generic CreateAPIView
Using docs you should do this from another way:
class CarSerializer(serializer.ModelSerializer):
photos = serializers.RelatedField(many=True)
class Meta:
model = Car
fields = ('id', 'name', 'photos',)
Maybe you can try this:
serializers.py
class CarImageSerializer(serializers.ModelSerializer):
class Meta:
model = CarImage
class CarSerializer(serializers.HyperlinkedModelSerializer):
photos = serializers.HyperlinkedRelatedField(many=True,
view_name='carimage-list')
class Meta:
model = Car
fields = ('id', 'name', 'photos',)
views.py
class CarImageList(ListCreateAPIView):
queryset = CarImage.objects.all()
serializer_class = CarImageSerializer
class CarList(ListCreateAPIView):
queryset = Car.objects.all()
serializer_class = CarSerializer
urls.py
url(r'^carimage/$', CarImageList.as_view(), name='carimage-list'),
url(r'^car/$', CarList.as_view(), name='car-list'),
You should take care about all needed imports. No guarantee, but you could give it a try.

Django REST Framework - query limit on nested serializer?

I have a situation in which one table is related to another via a foreign key as follows:
models.py
class Container(models.Model):
size = models.CharField(max_length=20)
shape = models.CharField(max_length=20)
class Item(models.Model):
container = models.ForeignKey(Container, related_name='items')
name = models.CharField(max_length=20)
color = models.CharField(max_length=20)
serializers.py
class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = Item
class ContainerSerializer(serializers.ModelSerializer):
items = ItemSerializer(many=True)
class Meta:
model = Container
fields = ('size', 'shape', 'items')
This works fine, but my problem is that all the items in the container get serialized. I only want items with color='green' to be serialized.
class ContainerSerializer(serializers.ModelSerializer):
items = serializers.SerializerMethodField()
def get_items(self, obj):
query = Item.objects.filter(item_set__color='green')
serializer = ItemSerializer(query, many=True)
return serializer.data
class Meta:
model = Container
fields = ('size', 'shape', 'items')
Instead of changing how serializer works, a simplier way, its just filter you Container with green color items and them try to serialize it
You can do something like this:
container_objects = Container.objects.filter(id='your_container_id',item_set__color='green')
serialized_containers = YourContainerSerializer(data=container_objects)

Limit choices to foreignkey in django rest framework

How to limit images of request.user to be linked with node. I wish I could do something like:
photo = models.ForeignKey(
Image,
limit_choices_to={'owner': username},
)
but request.user rather than username and I don't want to use local threads.
models.py
class Node(models.Model):
owner = models.ForeignKey(User)
content = models.TextField()
photo = models.ForeignKey(Image)
class Image(models.Model):
owner = models.ForeignKey(User)
file = models.ImageField(upload_to=get_upload_file_name)
serializers.py
class ImageSerializer(serializers.ModelSerializer):
owner = serializers.Field('owner.username')
class Meta:
model = Image
fields = ('file', 'owner')
class NodeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Node
fields = ('content', 'photo', 'owner')
I would deal with this by overriding get_serializer_class to dynamically return a serializer class at runtime, setting the choices option on the field there:
def get_serializer_class(self, ...):
user = self.request.user
owner_choices = ... # However you want to restrict the choices
class ImageSerializer(serializers.ModelSerializer):
owner = serializers.Field('owner.username', choices=owner_choices)
class Meta:
model = Image
fields = ('file', 'owner')
return ImageSerializer
You can create a custom foreign key field and define get_queryset() method there to filter related objects to only those of your user. The current user can be retrieved from the request in the context:
class UserPhotoForeignKey(serializers.PrimaryKeyRelatedField):
def get_queryset(self):
return Image.objects.filter(owner=self.context['request'].user)
class NodeSerializer(serializers.HyperlinkedModelSerializer):
photo = UserPhotoForeignKey()
class Meta:
model = Node
fields = ('content', 'photo', 'owner')
This example is using Django REST Framework version 3.
class CustomForeignKey(serializers.PrimaryKeyRelatedField):
def get_queryset(self):
return Table.objects.filter(is_active=True)
class Serializer(serializers.ModelSerializer):
(...)
table= CustomForeignKey()
class Meta:
(...)
even more easy is :
class Serializer(serializers.ModelSerializer):
(...)
table = serializers.PrimaryKeyRelatedField(queryset=Table.objects.filter(is_active=True))
class Meta:
(...)
Because I am sure this logic will be used across an entire Django application why not make it more generic?
class YourPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
def __init__(self, **kwargs):
self.model = kwargs.pop('model')
assert hasattr(self.model, 'owner')
super().__init__(**kwargs)
def get_queryset(self):
return self.model.objects.filter(owner=self.context['request'].user)
serializers.py
class SomeModelSerializersWithABunchOfOwners(serializers.ModelSerializer):
photo = YourPrimaryKeyRelatedField(model=Photo)
categories = YourPrimaryKeyRelatedField(model=Category,
many=True)
# ...
from rest_framework import serializers
class CustomForeignKey(serializers.PrimaryKeyRelatedField):
def get_queryset(self):
return Table.objects.filter(user=self.context['request'].user)
# or: ...objects.filter(user=serializers.CurrentUserDefault()(self))
class Serializer(serializers.ModelSerializer):
table = CustomForeignKey()