Django:总和外键数据的总值

时间:2017-12-10 08:17:07

标签: python django django-models django-templates django-views

我正在使用Django 1.11和Python3.5 我创建了一张桌子。这是截图。 enter image description here

当我从查询数据库中获得一个表时,它显示10 + 2 + 3 ...但是我想获得 Due Taka更新 <中每个客户的总和值/ strong>表中的列像这样10 + 2 + 4 + 2 = 18

这是我的model.py文件

class CustomerInfo(models.Model):
    customer_name = models.CharField('Customer Name', max_length=100)
    customer_mobile_no = models.CharField(
        'Mobile No', null=True, blank=True, max_length=12)
    customer_price=models.IntegerField('Customer Price',default=1)
    customer_product_warrenty = models.CharField('Product Warrenty',null=True, blank=True,max_length=10)
    customer_sell_date = models.DateTimeField('date-published', auto_now_add=True)
    customer_product_id=models.CharField('Product ID',max_length=300,null=True, blank=True)
    customer_product_name=models.TextField('Product Name')
    customer_product_quantity=models.IntegerField('Quantity',default=1)

    customer_uid = models.CharField(max_length=6, blank=True, null=True)
    customer_info=models.TextField('Customer Details Informations', blank=True, null=True)

    customer_conditions=models.CharField('Conditions',blank=True, null=True, max_length=300)

    customer_due_taka_info=models.IntegerField(default=0)
    customer_discount_taka=models.IntegerField(default=0)


    customer_first_time_payment=models.IntegerField('First Time Payment',default=0)
    customer_first_due_info = models.CharField('First Due Info',default='No due info', max_length=200, blank=True, null=True)

    customer_product_mrp=models.IntegerField('Products MRP', default=0)

    customers_refrence=models.CharField(max_length=100)

    customer_updated = models.DateTimeField(auto_now=True)
    customer_type=models.CharField('Customer Type', default='MobilePhone', max_length=50)






    def __str__(self):
        return self.customer_name

    def remainBalance(self):
        if self.customer_price > self.customer_due_taka_info:
            remain=self.customer_price - self.customer_due_taka_info
            return remain


    def totalRetalsPerSingle(self):
        return self.customer_product_quantity * self.customer_product_mrp





    #def product_warrenty(self):
        #expire_date_oneyr =self.customer_sell_date+ datetime.timedelta(days=365)
        #return 'This product expire on this date ' + str(expire_date_oneyr)

    class Meta:
        verbose_name = ("গ্রাহকের তথ্য")
        verbose_name_plural = ("গ্রাহকের তথ্যসমূহ")




#intregated with Customerinfo Model (Using foreignKey)
class DueTaka(models.Model):

    customer_due = models.IntegerField('Due Taka', default=0)
    customer_due_date=models.DateTimeField(auto_now_add=True)
    customer_due_info=models.CharField('Due Info', max_length=200, blank=True, null=True)
    customerinfo = models.ForeignKey(CustomerInfo, on_delete=models.CASCADE)
    due_customer_updated = models.DateTimeField(auto_now=True)

    def __int__(self):
        return self.customer_due

    def sum_total(self):
        return self.customer_due

这是我的views.py文件

due_update_info = CustomerInfo.objects.filter(duetaka__customer_due_date__range=(starts_date, tomorrow)).order_by('-customer_updated')

我想在每个客户的 DueTaka 模型中获得总价 customer_due 字段。如果客户名称是&#39; asad&#39;。他有一些多重的应付塔卡,他支付了多次这样的10 + 20 + 20现在我希望总价值达到50

如果我更新了客户的个人资料,那么同样的客户名称将再次出现在我的表中。但我不想要这个。 enter image description here

这是我的html模板文件

<h1>Due Paid Book</h1>
    <table class="table table-hover table-bordered">
        <p style="font-size:16px;">Due Paid Tody</p>
        <thead>
            <tr>
                <th>No</th>
                <th>Name</th>

                <th>Invoice ID</th>

                <th>Mobile</th>
                <th>Product</th>
                <th>Product MRP</th>
                <th>Customer Paid (TK)</th>
                <th>Due Amount</th>
                <th>Total Price</th>

                <th>Warrenty</th>
                <th>Purchase Date</th>
                <th>Due Taka update</th>
                <th>Update Date</th>
                <th>QN</th>
            </tr>
        </thead>

        {% for x in due_update_info %}
        <tbody>
            <tr>
.......code in here.......
                <td> 
                    {% for y in x.duetaka_set.all %}
                    {{y.customer_due | intcomma}}+
                    {% endfor %}
                    {{x.duetaka_set }}
                    <!--i want to toatal value in here, now nothing showing-->
                    {{total_price}}


                </td>



            </tr>
            <tr>

            </tr>
        </tbody>



        {% endfor %}
        <td colspan="10"></td>
        <td> Total Du Paid <br> <i>{{starts_date}} to {{end_date}}</i> </td>
        <td> <b>{{dupaid_today|intcomma}} TK</b> </td>

    </table>

现在我如何在视图文件中实现?

1 个答案:

答案 0 :(得分:1)

可能会像这样更改过滤器查询:

CustomerInfo.objects.filter(duetaka__customer_due_date__range=(starts_date, tomorrow)).annotate(due_taka_total=Sum('duetaka__customer_due')).order_by('-customer_updated')

将为您提供额外的字段'due_taka_total',可以在模板中使用,例如:

{{ y.due_taka_total }} 

假设y是CustomerInfo的对象

您可以编写自定义模板标记并使用它来查找总计:

from django import template
register = template.Library()

@register.simple_tag
def get_total_taka(customer_info_object):
    return sum([each.customer_due for each in customer_info_object.duetaka_set.all()])

并在模板中使用它:

{% get_total_taka y %}

假设y是Customerinfo的对象

要编写自定义模板代码,请参阅:Writing Custom Tags