hpw to create related model in django rest framework logic

Solutions on MaxInterview for hpw to create related model in django rest framework logic by the best coders in the world

showing results for - "hpw to create related model in django rest framework logic"
Vanessa
13 Jul 2019
1class OrderSerializer(serializers.ModelSerializer):
2    class Meta:
3        model = Order
4        fields = (
5            'id',
6            'total',
7            'discount',
8        )
9
10    def calculate_discount(self, whatever_params):
11        # calculate discount if you need... and return it
12
13    def calculate_tax(self, whatever_params):
14        # calculate tax amount if you need...and return it
15
16    def calculate_grand_total(self, whatever_params):
17        # calculate grand total if you need and return it
18
19    def create(self, validated_data):
20        # You can make an order by applying 
21        # some logic in the calculation method.
22        # Maybe by adding a bit of the context 
23        # you sent as parameters from view.