Django Rest Framework: serialize / deserialize computed field

I just started using Django and Django REST. I have the following models:

class Account(models.Model):
    name = models.CharField(max_length=100, blank=False)
    vat_perc = models.DecimalField(max_digits=4, decimal_places=2)
    def __str__(self):
        return "ACCOUNT: {0} -- {1}".format(self.name, str(self.vat_perc))


class Entry(models.Model):
    account = models.ForeignKey(Account)
    description = models.CharField(max_length=100)
    taxable_income = models.DecimalField(max_digits=10, decimal_places=2)
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, null=True)

    def save(self, *args, **kwargs):
        selected_vat = Account.objects.get(pk=self.account).vat_perc
        self.total_amount = self.taxable_income * (100.00+selected_vat)/100.00
        super(Entry, self).save(*args, **kwargs)

The idea would be to read the value vat_percinside the record accountthat the user just selected and perform a calculation to determine the value total_amountthat should then be stored in the record entryon (I know that some people consider this suboptimal due to duplicate data in the database please follow me anyway).

total_amount . , save , . , total_amount read_only.

:

class AccountSerializer(serializers.ModelSerializer):
    class Meta:
        model = Account
        fields = ('id', 'name', 'vat_perc',)


class EntrySerializer(serializers.ModelSerializer):
    class Meta:
        model = Entry
        fields = ('id', 'account', 'description', 'taxable_income', 'total_amount',)

    total_amount = serializers.ReadOnlyField()
    # alternatively: total_amount = serializers.FloatField(read_only=True)

, :

TypeError Entry.objects.create(). , , Entry.objects.create(). , EntrySerializer.create(), . : int() , , " ".

. - ? ? .

+4
1

Claudiu. SlugRelatedField decimal.Decimal float, . :

class Account(models.Model):
    name = models.CharField(max_length=100, blank=False)
    vat_perc = models.DecimalField(max_digits=4, decimal_places=2)
    def __str__(self):
        return "ACCOUNT: {0} -- {1}".format(self.name, str(self.vat_perc))


class Entry(models.Model):
    account = models.ForeignKey(Account)
    description = models.CharField(max_length=100)
    taxable_income = models.DecimalField(max_digits=10, decimal_places=2)
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, null=True)

    def save(self, *args, **kwargs):
        self.total_amount = self.taxable_income * (decimal.Decimal(100.00) + self.account.vat_perc) / decimal.Decimal(100.00)
        super(Entry, self).save(*args, **kwargs)

serializers.py

class AccountSerializer(serializers.ModelSerializer):
    class Meta:
         model = Account
         fields = ('id', 'name', 'vat_perc',)


class EntrySerializer(serializers.ModelSerializer):
    class Meta:
        model = Entry
        fields = ('id', 'account', 'description', 'taxable_income', 'total_amount',)

    total_amount = serializers.ReadOnlyField()
    account = serializers.SlugRelatedField(queryset=Account.objects.all(), slug_field="vat_perc")
+3

Source: https://habr.com/ru/post/1584789/


All Articles