Kaynağa Gözat

Added related name and showing read only item details in invoice list

master
kj1352 3 yıl önce
ebeveyn
işleme
66a04f3025
2 değiştirilmiş dosya ile 18 ekleme ve 2 silme
  1. +14
    -1
      invoice/models.py
  2. +4
    -1
      invoice/serializers.py

+ 14
- 1
invoice/models.py Dosyayı Görüntüle

@@ -1,5 +1,6 @@
from django.db import models
from project.models import Project
from client.models import Client, Address, TaxType


class Invoice(models.Model):
@@ -19,6 +20,18 @@ class Invoice(models.Model):
total += item.item_total
return total

@property
def invoice_total_after_tax(self):
items = Item.objects.filter(invoice=self.pk)
project = Project.objects.get(client=self.project)
client = Client.objects.get(pk=project.client)
address = Address.objects.get(pk=client.address)
tax_type = TaxType.objects.get(pk=address.tax_type)
total = 0
for item in items:
total += item.item_total
return total

def __str__(self):
return self.invoice_id

@@ -27,7 +40,7 @@ class Item(models.Model):
name = models.TextField(blank=False, null=False)
amount = models.DecimalField(max_digits=15, decimal_places=2)
quantity = models.IntegerField()
invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE)
invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE, related_name='items')

@property
def item_total(self):


+ 4
- 1
invoice/serializers.py Dosyayı Görüntüle

@@ -9,6 +9,9 @@ class ItemSerializer(serializers.ModelSerializer):


class InvoiceSerializer(serializers.ModelSerializer):
items = ItemSerializer(many=True, read_only=True)

class Meta:
model = Invoice
fields = ['id', 'title', 'project', 'invoice_id', 'raised_date', 'due_date', 'paid_date', 'is_archived', 'invoice_total']
fields = ['id', 'title', 'project', 'invoice_id', 'raised_date', 'due_date', 'paid_date', 'is_archived',
'invoice_total', 'items']