![]() ![]() It’s hard to imagine how it could support all possible types of nested insertions and deletions. By default, DRF doesn’t support multiple updates at all. It comes with a trade-off: there is a smaller number of requests to process at the cost of a longer processing time. Things can get a little tricky when you have to support multiple creates, updates and deletes in nested serializers within one high-level serializer. Most of the time serializers are completely straightforward and with some experience, there’s nothing that could go wrong. Handling multiple creates/updates/deletes in nested serializers If in your view you want to serialize data that will be transmitted outside of your API, this is how you do it: In these terms we can distinguish 3 types of serializers: create, update and retrieve. The way it is initialized determines the action that it will fulfill. There is a handful of functionalities connected with serializers that you might want to know.Įvery serializer can be used for both reading and writing operations. Understanding different types of serializersĪs a DRF user you don’t need to bother with views and url configurations, so you will probably pay most of your attention to serializers, which act as translators between Django model instances and their representations such as json. Now that writing views is finished, you’ve saved enough time to have a cup of coffee. You can even add some custom action to your viewset using decorator. retrieve one of the tags by GET v1/tag/.list all your tags by sending GET request to v1/tag/,.Tracks = serializers.Now your viewset is functional enough that you can: ![]() ![]() StringRelatedField may be used to represent the target of the relationship using its _str_ method.įor example, the following serializer: class AlbumSerializer(serializers.ModelSerializer): Return '%d: %s' % (self.order, self.title) class Album(models.Model):Īlbum_name = models.CharField(max_length=100)Īrtist = models.CharField(max_length=100)Īlbum = models.ForeignKey(Album, related_name='tracks', on_delete=models.CASCADE) Our models will be for music albums, and the tracks listed on each album. In order to explain the various types of relational fields, we'll use a couple of simple models for our examples. Owner = PrimaryKeyRelatedField(queryset=()) Name = CharField(allow_blank=True, max_length=100, required=False) ![]() Id = IntegerField(label='ID', read_only=True) To do so, open the Django shell, using python manage.py shell, then import the serializer class, instantiate it, and print the object representation… > from rializers import AccountSerializer Inspecting these automatically generated fields can be a useful tool for determining how to customize the relationship style. When using the ModelSerializer class, serializer fields and relationships will be automatically generated for you. Optimizing the queryset passed to AlbumSerializer with: qs = _related('tracks') If AlbumSerializer is used to serialize a fairly large queryset with many=True then it could be a serious performance problem. Print(AlbumSerializer(qs, many=True).data) # For each album object, tracks should be fetched from database It is the programmer's responsibility to optimize queries to avoid additional database hits which could occur while using such a serializer.įor example, the following serializer would lead to a database hit each time evaluating the tracks field if it is not prefetched: class AlbumSerializer(serializers.ModelSerializer):įields = A serializer with a field spanning an orm relation through its source attribute could require an additional database hit to fetch related objects from the database. Note: REST Framework does not attempt to automatically optimize querysets passed to serializers in terms of select_related and prefetch_related since it would be too much magic. Note: The relational fields are declared in relations.py, but by convention you should import them from the serializers module, using from rest_framework import serializers and refer to fields as serializers. They can be applied to ForeignKey, ManyToManyField and OneToOneField relationships, as well as to reverse relationships, and custom relationships such as GenericForeignKey. Relational fields are used to represent model relationships. Data structures, not algorithms, are central to programming. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |