СПИСОК ДОЛЖНИКОВ DJANGO
У меня есть две модели с одинаковыми именами, я хочу отобразить их значения (отображать повторяющиеся имена один раз) и их суммирование. то, что я достиг до сих пор, это сделать только для таблицы Invoice и работает идеально, но когда я хочу добавить receiptmodel не упорядочивается так, как упорядочивается invoice
model.py
from django.db import models
class invoice(models.Model):
customer = models.CharField(max_length=100, null=False)
total_amount = models.DecimalField(max_digits=20, decimal_places=2)
class Meta:
db_table="invoice"
class ReceiptModel(models.Model):
customer = models.CharField(max_length=100, blank=True, unique=False)
to_amount = models.CharField(max_length=100, blank=True, unique=False)
class Meta:
db_table="receiptmodel"
view.py
from django.shortcuts import render, redirect, get_object_or_404, reverse
from django.contrib import messages
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.forms import modelformset_factory
#from django.forms import ModelForm
from .models import *
from .forms import *#UserRegisterForm, InvoiceForm, LineItemFormset, NewCustomerForm, CustomerReceiptForm
from django.urls import *
from itertools import chain
from operator import attrgetter
from django.db.models import Sum, Q
def ReceivableReport(request): #still not working
receivable_inv = Invoice.objects.values("customer").annotate(totinv= Sum('total_amount'))
receivable_rec = ReceiptModel.objects.values("customer").annotate(totrec= Sum('to_amount'))
#zipped_data = zip(receivable_inv, receivable_rec)
#zipped_data = list(chain(receivable_inv,receivable_rec))
context = {
'receivable_inv':receivable_inv,
'receivable_rec':receivable_rec,
# 'zipped_data ':zipped_data,
}
return render(request, "reports/debtorlist.html", context)
debtorlist.html
<table style="width: 50%;">
<thead>
<tr>
<th>Name</th>
<th>Inv Amount</th>
<th>Rec Amount</th>
</tr>
</thead>
{% for receivable_inv in receivable_inv%}
<tbody>
<tr>
<td>{{ receivable_inv.customer }}</td>
<td>{{ receivable_inv.totinv}}</td>
<td>{{ receivable_rec.totrec}}</td>
</tr>
</tbody>
{% endfor %}
</table>
output i want
-----------------------------------------------------
|names | invoice amount | receipt amount | balance |
------------------------------------------------------
|per1 | 600 | 400 | 200 |
|per2 | 700 | 300 | 400 |
|per3 | 560 | 160 | 400 |
|per4 | 340 | 240 | 100 |
|per5 | 238 | 38 | 200 |
------------------------------------------------------
output i get
-----------------------------------------------------
|names | invoice amount | receipt amount | balance |
------------------------------------------------------
|per1 | 600 | | |
|per2 | 700 | | |
|per3 | 560 | | |
|per4 | 340 | | |
|per5 | 238 | |
------------------------------------------------------
это означает, что сумма счета работает правильно, но мне не удалось добавить receiptmodel с теми же именами. Когда я пытаюсь использовать list(chain(table1,table2)
нет вывода в шаблоне
<
Вернуться на верх
def ReceivableReport(request): #still not working
receivable_inv = Invoice.objects.annotate(totinv= Sum('total_amount')).values('customer', 'totinv')
receivable_rec = ReceiptModel.objects.annotate(totrec= Sum('to_amount')).values('customer', 'totrec')
#zipped_data = zip(receivable_inv, receivable_rec)
#zipped_data = list(chain(receivable_inv,receivable_rec))
context = {
'receivable_inv':receivable_inv,
'receivable_rec':receivable_rec,
# 'zipped_data ':zipped_data,
}
return render(request, "reports/debtorlist.html", context)
Попробуйте так: