Reputation: 11543
I understand that it is possible to add a models method or property to a serializer, like this:
class Order(models.Model):
...
def tax_status(self, check_item_bought=True):
...
So, to add total_tax
to an OrderSerializer
, it is as simple as this:
class OrderSerializer(serializers.ModelSerializer):
tax_status = serializers.CharField(required=False)
class Meta:
model = Order
fields = ["tax_status", ...]
The above works well. However, I need to add another tax_status_all
field to the serializer that points to the same method but setting the arg check_item_bought
to False. How can I do this? Any advice will help.
Upvotes: 2
Views: 2444
Reputation: 1386
For this goal you can use SerializerMethodField
class YourSerializer(serializers.Serializer):
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)
Upvotes: 5