Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a custom serializer?

I want to perform some data manipulations before sending back a JSON response with DRF.

Situation

My model is:

class ThirdParty(models.Model):
    label = models.CharField(verbose_name=_("Third party label"), null=False, blank=False, default=DEFAUT_LABEL, max_length=255)

class CashFlow(TimeStampedModel):
    date = models.DateField(verbose_name=_("Due date"), null=True, blank=True)
    forecasted_value = models.DecimalField(verbose_name=_("Forecasted value"), null=True, blank=True, max_digits=11, decimal_places=2)
    third_party = models.ForeignKey(ThirdParty, null=False, blank=False, related_name='cashflows')

Currently I have two serializers:

class CashFlowSerializer(serializers.ModelSerializer):
    third_party = serializers.PrimaryKeyRelatedField(many=False, read_only=True, allow_null=True)
    class Meta:
        model = CashFlow
        fields = ('id', 'date', 'forecasted_value', 'edited_value', 'third_party')

class ThirdPartyReadSerializer(serializers.ModelSerializer):
    cashflows = CashFlowSerializer(many=True, read_only=True)
    class Meta:
        model = ThirdParty
        fields = ('id', 'label', 'category', 'cashflows',)

And my ThirdParty view is correctly returning a nice JSON as:

{
        "id": 15,
        "label": "Adeo",
        "category": 7,
        "cashflows": [
            {
                "id": 1,
                "date": "2016-11-01",
                "forecasted_value": "2000.00",
                "edited_value": null,
                "third_party": 15
            },
            {
                "id": 2,
                "date": "2017-01-17",
                "forecasted_value": "3000.00",
                "edited_value": null,
                "third_party": 15
            },
            {
                "id": 3,
                "date": "2017-01-31",
                "forecasted_value": "1000.00",
                "edited_value": null,
                "third_party": 15
            }
        ]
    }

Question

I want to group the cash flows by month and add their values. Question is: what is the best way to do it?

The expected result is:

{
        "id": 15,
        "label": "Adeo",
        "category": 7,
        "cashflows": [
            {
                "date": "2016-11-01",
                "forecasted_value": "2000.00",
                "edited_value": null,
                "third_party": 15
            },
            {
                "date": "2017-01-01",
                "forecasted_value": "4000.00",
                "third_party": 15
            }
        ]
    }

And that will be a read-only serializer.

like image 409
bixente57 Avatar asked Nov 22 '16 17:11

bixente57


1 Answers

Use the serializer's to_representation:

def to_representation(self, obj):
    data = super().to_representation(obj)
    # manipulate data['cashflows'] to group by month
    return data
like image 115
lucasnadalutti Avatar answered Nov 03 '22 08:11

lucasnadalutti