Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit number of records in Django Rest Framework reverse relations

I am getting started with Django Rest Framework, and it's behaving quite well. I got all the stuff working as I wanted it to be. I have came across a problem that I am not getting answer of.

I am using reverse relationship.

Models

class Province(models.Model):
    name = models.CharField(max_length=50)
    intro = models.CharField(max_length=1000, null=True, blank=True)
    description = models.TextField(max_length=10000, null=True, blank=True)

class Picture(models.Model):
    name = models.TextField("Title", max_length=10000, null=True, blank=True)
    pro = models.ForeignKey(Province, verbose_name="Province")

When I write the reverse relationship serializers of Province, e.g. for a single province.

Views

ProToPicturesSerial(node, many=False).data

I get all the pictures in this province. I want to get some number of pictures, maybe last 3, or the 5 pictures that are added lately.

How can I limit the number of picture instance? Because as the number grows in the picture records, the application will tend to get slower.

Serializer

class ProToPicturesSerial(serializers.ModelSerializer):
    pro_pictures = PictureSerializer(many=True)

    class Meta:
        model = Province
        fields = ('id', 'name', 'intro', 'description', 'pro_pictures')

Let me know if I am missing something obvious.

like image 844
A.J. Avatar asked Feb 20 '14 09:02

A.J.


People also ask

What is reverse in Django REST framework?

reverse. Signature: reverse(viewname, *args, **kwargs) Has the same behavior as django. urls. reverse , except that it returns a fully qualified URL, using the request to determine the host and port.

What is the difference between ModelSerializer and HyperlinkedModelSerializer?

The HyperlinkedModelSerializer class is similar to the ModelSerializer class except that it uses hyperlinks to represent relationships, rather than primary keys. By default the serializer will include a url field instead of a primary key field.

How to manage paginated data in Django with REST framework?

Django provides a few classes that help you manage paginated data – that is, data that’s split across several pages, with “Previous/Next” links. REST framework includes support for customizable pagination styles. This allows you to modify how large result sets are split into individual pages of data. The pagination API can support either:

What are some websites that use Django REST framework?

There are many websites such as Instagram, Pinterest, Youtube, Spotify, and Bitbucket that use this framework. This post assumes a basic familiarity with Django REST Framework. We would discuss 10 useful tips to increase your Django REST coding efficiency.

Why is the request user not available in Django REST framework?

Due to some design decisions in Django Rest Framework, the request.user is not available in the middleware request layer but only the views layer. Here is the best solution from StackOverflow.

How to create a relational field in Django?

To do so, open the Django shell, using python manage.py shell, then import the serializer class, instantiate it, and print the object representation… In order to explain the various types of relational fields, we'll use a couple of simple models for our examples.


1 Answers

You can point the source attribute of PictureSerializer to a method on province that returns only 3 related pics:

class ProToPicturesSerial(serializers.ModelSerializer):
    pro_pictures = PictureSerializer(many=True, source='first_three_pics')

    class Meta:
        model = Province
        fields = ('id', 'name', 'intro', 'description', 'pro_pictures')

and

class Province(models.Model):
    name = models.CharField(max_length=50)
    intro = models.CharField(max_length=1000, null=True, blank=True)
    description = models.TextField(max_length=10000, null=True, blank=True)

    def first_three_pics(self):
        return self.picture_set.all()[:3]
like image 159
almalki Avatar answered Sep 24 '22 02:09

almalki