Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 28, 2021 05:19 am GMT

Django Rest Framework Tips and Tricks

Original Post in https://skamalakannan.dev/blog/django-rest-framework-tips

Django Rest Framework is a powerful tool for creating API, and it comes with a lot of features. Here is the list of tips that will help in developing your API better.

Sending common field in all the responses

We might have use cases where we want to send a lot in all our responses, like resource URI, create date, or created user kind of fields.We can use serializer mixins for this purpose. Mixins are abstract classes, and we can define all the variables in these mixins, and we can inherit these mixins to the actual class.

from user.serializers import UserMinimumSerializerclass CreatedModifiedSerializer(serializers.Serializer):    created_date = serializers.DateTimeField(read_only=True)    created_by = UserMinimumSerializer(read_only=True)    modified_date = serializers.DateTimeField(read_only=True)    modified_by = UserMinimumSerializer(read_only=True)    class Meta:        abstract = Trueclass OrderSerializer(CreatedModifiedSerializer):    class Meta:        model = Order        fields = '__all__'

In the above example, we have created a CreatedModifiedSerializer as a mixin. It will get applied to the OrderSerializer, if the Order doesn't have any one of the fields declared in the mixin, we will receive an exception.

Return computed value from the model

if you have a scenario is to send the computed value. We can create a property for a model and specify the property name directly in the Serializer class.

class Order(models.Model):    qty = models.IntegerField()    price_per_qty = models.DecimalField(max_digits=5, decimal_places=2, default=0.0)    @property     def total_amount(self):        return self.qty * self.price_per_qtyclass OrderSerializer(models.ModelSerializer):    class Meta:        model = Order        fields = ('qty', 'price_pert_qty', 'total_amount') #Total amount is property of the model

Since the total_amount is a property, it will pick it directly. We can also declare the field type, specify as read-only, or result in an AttributeError exception.

Saving nested serializer

If we want to create or update a nested model relation. drf-writable-nested is a package that comes for rescue; it supports nested serializer both create and update operations. it also available supports for OneToOne, ForeignKey, ManyToMany.
https://github.com/beda-software/drf-writable-nested

from drf_writable_nested.serializers import WritableNestedModelSerializerfrom rest_framework.serializers import ModelSerializerclass OrderItemSerializer(ModelSerializer):    class Meta:        model = OrderItems        fields = '__all__'class OrderSerializer(WritableNestedModelSerializer):    items = OrderItemSerializer(many=True)    class Meta:        model = Order        fields = '__all__'

Accept object id while create and update, return object through serializer

By default, DRF accepts and returns the field in the same format. If the use case is to accept the id update and create but return the serialized object. We can use PresentablePrimaryKeyRelatedField from the drf-extra-fields package for this purpose.
The custom field takes two arguments one is the queryset of the model, and another one presentation_serializer is a serializer class for the model.

from drf_extra_fields.relations import PresentablePrimaryKeyRelatedFieldfrom rest_framework.serializers import ModelSerializerclass OrderSerializer(ModelSerializer):    items = OrderItemSerializer(many=True)    order_type = PresentablePrimaryKeyRelatedField(        queryset = OrderType.objects.all(),        presentation_serializer=OrderTypeSerializer    )    class Meta:        model = Order        fields = '__all__'

drf-extra-fields has many more custom fields, like Base64ImageField, it will accept a image file
as base64 encoded and will return as image file. please checkout the https://github.com/Hipo/drf-extra-fields for more information


Original Link: https://dev.to/srkama/django-rest-framework-tips-and-tricks-276b

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To