django 如何从两个不同的模型中获取聚合值并在模板中显示它们

5sxhfpxr  于 2023-05-08  发布在  Go
关注(0)|答案(1)|浏览(117)

models.py

# Customer Model

class Customer(models.Model):
    name =models.CharField(max_length=100)
    primary_contact = models.CharField(max_length=10)
    secondary_contact = models.CharField(max_length=10, blank=True)
    address = models.TextField(blank=True)

def __str__(self):
    return self.name

def get_absolute_url(self):
    return reverse('customers')

# Issue Model

class Issue(models.Model):
    date_time = models.DateTimeField(default=datetime.now)
    date = models.DateField(auto_now_add = True)
    customer = models.ForeignKey(Customer, on_delete = models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE, )
    quantity = models.PositiveIntegerField()
    kt = models.IntegerField()
    rate = models.FloatField()
    vat = models.IntegerField(default=15)

@property
def kt_21(self):
    return round(self.kt / 21 * self.quantity , 2)

@property
def vat_amt(self):
    return self.quantity * self.rate * self.vat/100

@property
def total_amt(self):
    return self.quantity * self.rate + self.vat_amt

def get_absolute_url(self):
    return reverse('issue-hist')

def __str__(self):
    return  str(self.customer) + " - "+ str(self.total_amt)+' SAR'
# Receive Model

class Receive(models.Model):
    date_time = models.DateTimeField(default=datetime.now)
    date = models.DateField(auto_now_add = True)
    customer = models.ForeignKey(Customer, on_delete = models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE, )
    quantity = models.PositiveIntegerField()
    kt = models.IntegerField()
    rate = models.FloatField()
    vat = models.IntegerField(default=15)

    @property
    def kt_21(self):
        return round(self.kt / 21 * self.quantity , 2)
    
    @property
    def vat_amt(self):
        return self.quantity * self.rate * self.vat/100

    @property
    def total_amt(self):
        return self.quantity * self.rate + self.vat_amt
    
    def get_absolute_url(self):
        return reverse('rec-hist')

    def __str__(self):
        return  str(self.customer) + " - "+ str(self.total_amt)+' SAR'

我有这些模型,现在我想要的是目前的总数量发出和收到的客户和余额。为此,我试图创造一个这样的观点
views.py(This只是文件中的一个视图)

class CustomerBalanceDetailView(DetailView):
    model = Customer
    template_name = 'customer/balances.html'
    context_object_name = 'cust'
    ordering = [Lower('name')]

    def balance(self, request, pk):
    

        custo = Customer.objects.get(id = pk)
        issue = Issue.objects.filter(customer = custo)
        tot = issue.aggregate(iss = Sum('quantity'))
                        
        return render(request, 'customer/balances.html',context={'total':tot.iss})

现在我只尝试输出总发行量,我尝试在shell中使用这些查询,它们工作,但由于某种原因,上下文没有在模板中传递。
HTML模板

{% extends 'base2.html' %}
{% load widget_tweaks %}
{% load static%}

{% block css %} 
    <link rel="stylesheet" href="{% static 'utility.css' %}"> 
    <link rel="stylesheet" href="{% static 'customer/cust_d.css' %}"> 

{% endblock css %}


{% block content %}    

    <!-- Headers -->
    <h1 class="c_name h">Name</h1>
    <h1 class="cont h">Amount Issued</h1>
    <h1 class="s_cont h">Amount Received</h1>
    <h1 class="add h">Balance </h1>

    <!-- Product List -->
    <div class="flex-wrap">
        
            <div class="bar name">
                <p class="p1">{{cust.name}}</p>
                <p class="p2">{{total}}</p>

            </div>
        
    </div>

    <!-- Update & Delete Buttons -->

{% endblock content %}
wnvonmuf

wnvonmuf1#

解决方案是不使用class based view。只需要使用function based view并在模板中传递上下文"dot.nottaion"
更正的文件:
www.example.com无变更models.py
views.py

def balance(request, pk):
    cust = Customer.objects.get(id = pk)

    # Calculating Issue
    issue = Issue.objects.filter(customer = cust)
    tot_issue = issue.aggregate(iss = Sum('quantity'))
    if tot_issue['iss'] == None:
        tot_issue['iss'] = 0

    # Calculating Receive
    receive = Receive.objects.filter(customer = cust)
    tot_receive = receive.aggregate(rec = Sum('quantity'))
    if tot_receive['rec'] == None:
        tot_receive['rec'] = 0

    # Calculating Balance
    balance = tot_issue['iss'] - tot_receive['rec']
    
    context={'cust':cust, 'total_i':tot_issue,'total_r':tot_receive, 'balance':balance }
    
    return render(request, 'customer/balances.html', context)

我删除了类视图,现在将不得不手动查询客户名称和所有。
HTML template

{% extends 'base2.html' %}
{% load widget_tweaks %}
{% load static%}

{% block css %} 
    <link rel="stylesheet" href="{% static 'utility.css' %}"> 
    <link rel="stylesheet" href="{% static 'customer/cust_d.css' %}"> 

{% endblock css %}


{% block content %}    

    <!-- Headers -->
    <h1 class="c_name h">Name</h1>
    <h1 class="cont h">Amount Issued</h1>
    <h1 class="s_cont h">Amount Received</h1>
    <h1 class="add h">Balance </h1>

    <!-- Product List -->
    <div class="flex-wrap">
        
            <div class="bar name">
                <p class="p1">{{cust.name}}</p>
                <p class="p2">{{total_i.iss}}</p>
                <p class="p3">{{total_r.rec}}</p>
                <p class="p4">{{balance}}</p>
            
            </div>
        
    </div>

    <!-- Update & Delete Buttons -->

{% endblock content %}

相关问题