想要添加每个国家/地区的所有金额

发布于 2024-12-05 10:39:17 字数 1186 浏览 0 评论 0原文

你好,我有一个处理销售/购买的 django 应用程序。我想要做的是根据国家类型添加金额。

查看销售表,有两种不同的国家类型。英国和欧盟(我知道欧盟不是一个国家,但没关系:))

models.py

COUNTRY_TYPE_CHOICES = (
        (1, 'UK'),
        (2, 'EU'),
        )
class Sale(models.Model):
    country_type = models.IntegerField(verbose_name = "Location", choices = COUNTRY_TYPE_CHOICES)
    date = models.DateField()
    amount = models.DecimalField(max_digits=20, decimal_places=2)
    description =  models.TextField(max_length = 400)
    def __unicode__(self):
        return unicode(self.amount)

现在我想显示所有销售额的金额。我想要两个结果。来自英国的所有金额的总和,以及来自欧盟的金额的总和。由于两种不同的选择类型,我有点困惑如何添加所有金额。

这也是我的视图文件,也可能有帮助。

views.py

def home(request):
    sales = Sale.objects.all()
    return render_to_response('home.html', {'sales':sales}, context_instance=RequestContext(request))

更新: 到目前为止,我已经

uk_sales = Sale.objects.filter(country_type='1')

{{uk_sales}}

在屏幕上完成了:<销售:467.99>,<销售:699.99>,<售价:499.99>]

现在,如果我可以添加所有这些值,那就太好了。不算他们。

Hello I have a django app dealing with Sales/purchases. What I want to do is add amounts depending on thier country type.

Looking at the sales table, there are two different country types. Uk and EU (I know EU is not a country but nevermind :) )

models.py

COUNTRY_TYPE_CHOICES = (
        (1, 'UK'),
        (2, 'EU'),
        )
class Sale(models.Model):
    country_type = models.IntegerField(verbose_name = "Location", choices = COUNTRY_TYPE_CHOICES)
    date = models.DateField()
    amount = models.DecimalField(max_digits=20, decimal_places=2)
    description =  models.TextField(max_length = 400)
    def __unicode__(self):
        return unicode(self.amount)

Now I want to display the amount of all sales. I want two results. The sum of all amount from the UK, and the sum of amount that are from the EU. I'm slight confused how you would add all amounts because of the two different choice types.

Here is also my views file which may help as well.

views.py

def home(request):
    sales = Sale.objects.all()
    return render_to_response('home.html', {'sales':sales}, context_instance=RequestContext(request))

Update: I have done so far

uk_sales = Sale.objects.filter(country_type='1')

{{uk_sales}}

On screen Gives me: <Sale: 467.99>, <Sale: 699.99>, <Sale: 499.99>]

Now It wold be good If I could add all these values. Not counting them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫轩蝶泪 2024-12-12 10:39:17
from django.db.models import Sum
Sale.objects.values('country_type').annotate(Sum('amount')) 
from django.db.models import Sum
Sale.objects.values('country_type').annotate(Sum('amount')) 
桃扇骨 2024-12-12 10:39:17

这项工作对我来说很安静。

uk_sales = Sale.objects.filter(country_type='1')
uk_amount = uk_sales.aggregate(price = Sum('amount'))['price']

This work quiet well for me.

uk_sales = Sale.objects.filter(country_type='1')
uk_amount = uk_sales.aggregate(price = Sum('amount'))['price']
猫性小仙女 2024-12-12 10:39:17

如果您使用的是 Django 1.1 或更高版本,则可以使用 Django Aggregate Support ,例如:

query_amount = Item.objects.extra(select={'sum': 'sum(amount)'}).values('sum', 'amount')
query_amount.query.group_by = ['country_type']

这是有关该主题的 Django 官方文档这是一个很好的教程

If you are using Django 1.1 or newer, the you can use Django Aggregate Support with something like:

query_amount = Item.objects.extra(select={'sum': 'sum(amount)'}).values('sum', 'amount')
query_amount.query.group_by = ['country_type']

Here's Django official documentation on the topic. And here's a nice tutorial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文