显示发票的所有项目 - django

Show all items of an invoice - django

我想获取属于发票的所有项目并将它们显示到模板但没有成功。到目前为止我所做的如下:

我有两个模型:

class Invoice(models.Model):

    PAYMENT_OPTIONS = (
        ('CASH', _('CASH')),
        ('DEPOSIT', _('DEPOSIT')),
        ('CARD', _('CARD')),
    )

    INVOICE_TYPE = (
        ('Service', _('Service')),
    )

    invoice_number = models.CharField(max_length=7, unique=True, default='INV4898')
    invoice_user = models.ForeignKey(Account, on_delete=models.CASCADE)
    invoice_type = models.CharField(max_length=30, choices=INVOICE_TYPE, default='Service')
    payment_option = models.CharField(max_length=30, choices=PAYMENT_OPTIONS)
    invoice_name = models.CharField(max_length=30, null=True, blank=True)
    vat = models.CharField(max_length=9, blank=True, null=True)
    gross_amount = models.DecimalField(max_digits=6, decimal_places=2)
    vat_amount = models.DecimalField(max_digits=6, decimal_places=2)
    net_amount = models.DecimalField(max_digits=6, decimal_places=2)
    created_date = models.DateTimeField(auto_now_add=True)
    modified_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
      return f"{self.invoice_user.first_name} {self.invoice_user.last_name} - {self.invoice_number}"

class Item(models.Model):

    invoice = models.ForeignKey(Invoice, related_name='items', on_delete=models.CASCADE)
    title = models.CharField(max_length=255)
    quantity = models.IntegerField(default=1)
    unit_price = models.DecimalField(max_digits=6, decimal_places=2, default=Decimal('0.00'))
    net_amount = models.DecimalField(max_digits=6, decimal_places=2, default=Decimal('0.00'))
    vat_rate = models.CharField(max_length=10, default=0)
    discount = models.DecimalField(max_digits=6, decimal_places=2, default=Decimal('0.00'))
    is_paid = models.BooleanField(default=False)
    created_date = models.DateTimeField(auto_now_add=True)
    modified_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
      return f"{self.invoice.invoice_number}"

在 views.py 中,我有一个列表视图,其中我试图获取基于 invoice_number 的所有发票的列表,然后将每个 invoice_number 匹配到项目发票。但是,使用下面的代码,第一次打印会带来所有 invoice_number(s),第二次打印会带来所有发票的所有项目,而不是每张发票的项目。

def list_invoices(request, userprofile_id):
    user_profile = get_object_or_404(Account, pk=userprofile_id)
    all_invoices =Invoice.objects.filter(invoice_user=user_profile)

    invoice_number = Invoice.objects.values_list('invoice_number')
    print(invoice_number)
    
    item_invoice = Item.objects.filter(invoice__invoice_number__in=invoice_number)
    print(item_invoice)


    context = {
        'all_invoices': all_invoices,
        'user_profile': user_profile,
        'item_invoice': item_invoice,
    }
    return render(request, 'invoices/list-invoices.html', context)

如有任何帮助,我将不胜感激。

您不需要获取 Item 而不是 Invoiceinvoice_number。您只需要传递 user_profile 的发票,最好也预取 Items:

def list_invoices(request, userprofile_id):
    user_profile = get_object_or_404(Account, pk=userprofile_id)
    all_invoices = Invoice.objects.filter(
        invoice_user=user_profile
    )<strong>.prefetch_related('items')</strong>

    context = {
        'all_invoices': all_invoices,
        'user_profile': user_profile
    }
    return render(request, 'invoices/list-invoices.html', context)

然后在模板中,可以访问.items,所以:

<ul>
{% for invoice in all_invoices %}
    <li> {{ invoice.invoice_number }}</li>
    <ul>
    {% for item in invoice.items.all %}
        <li>{{ item.quantity }} {{ item.title }}</li>
    {% endfor %}
    </ul>
{% endfor %}
</ul>