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.
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.
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.
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.
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.
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.
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:
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.
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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With