python - Django:外键数据的总值(value)

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

我正在使用 Django 1.11 和 Python3.5 我创建了一个表。这是一个屏幕截图。 enter image description here

当我从查询数据库中获取表格时,它显示 10+2+3...但我想在到期 Taka 更新<内获取每个客户的总值(value) 表中的列如下所示 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 字段。如果客户名称是“asad”。他有一些应付的塔卡,他支付了多次,像这样 10+20+20 现在我想得到像 50 这样的总值(value)

如果我更新客户的个人资料,相同的客户名称会再次出现在我的表中。但我不想要这个。 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>

现在如何在 View 文件中实现?

最佳答案

也许可以像这样更改过滤器查询:

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

关于python - Django:外键数据的总值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47737061/

相关文章:

javascript - AJAX 未按预期更新数据库查询

python - Django manytomany 信号?

python - 从特定索引开始的数据框 cummin 列

python - python : None type given, 中递归函数的返回和赋值需要一个 int 值

python - 遍历 Selenium 2 WebDriver 中的表(python)

Django 复选框字段

python - 为什么我在 Django Rest Framework 中得到 "AssertionError: May not set both ` read_only` 和 `required` “?

python - 如何只保留数据框列中的字符串

django - 如何使用 gunicorn 部署 Django + Whitenoise 应用程序?

django - 过滤最接近给定日期时间的日期时间