Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

drf model methods serializer

models.py
=========
class Order(models.Model):
    ...

    def tax_status(self, check_item_bought=True):
        ...
        

serializers.py
==========
class YourSerializer(serializers.ModelSerializer):
    tax_status = serializers.CharField(required=False)
    tax_status_all = serializers.SerializerMethodField()

    class Meta:
        model = Order
        fields = ("tax_status", "tax_status_all")

    def get_tax_status_all(self, obj):  # "get_" + field name
        return obj.tax_status(check_item_bought=False)
Source by www.django-rest-framework.org #
 
PREVIOUS NEXT
Tagged: #drf #model #methods #serializer
ADD COMMENT
Topic
Name
5+5 =