与Django Orm进行查询?

发布于 2025-01-21 10:40:22 字数 1478 浏览 2 评论 0原文

我已经使用此代码进行查询:

views.py:

        reservations_table = Reservation.objects.all()

        total_commission = 0

        for reservation in reservations_table:
            for city in COMMISSION_RATES.keys():
                if reservation.city == city.upper():
                    total_commission += reservation.net_income * COMMISSION_RATES[city] / 100

        for reservation in reservations_table:
            for month in COMMISSION_PER_MONTH:
                if reservation.checkout.month == month:
                    reservation.monthly = COMMISSION_PER_MONTH[month]

        for reservation in reservations_table_city:
            commission_amount_city += reservation.net_income * COMMISSION_RATES[form.cleaned_data['city']] / 100

models.py:

from django.db import models


class Reservation(models.Model):
    reservation_code = models.CharField(max_length=150,unique=True)
    checkin = models.DateField()
    checkout = models.DateField()
    flat = models.CharField(max_length=150)
    city = models.CharField(max_length=150)
    net_income = models.DecimalField(max_digits=10, decimal_places=2)

    def __str__(self):
        return self.reservation_code

startants.py:

COMMISSION_RATES = {
    'LONDON': 10,
    'PARIS': 12,
    'PORTO': 9,
}

如何进行查询但使用只有django orm?因此,有3个疑问可行,但是想法是使它们全部django orm。

I have used this code to make a query:

views.py:

        reservations_table = Reservation.objects.all()

        total_commission = 0

        for reservation in reservations_table:
            for city in COMMISSION_RATES.keys():
                if reservation.city == city.upper():
                    total_commission += reservation.net_income * COMMISSION_RATES[city] / 100

        for reservation in reservations_table:
            for month in COMMISSION_PER_MONTH:
                if reservation.checkout.month == month:
                    reservation.monthly = COMMISSION_PER_MONTH[month]

        for reservation in reservations_table_city:
            commission_amount_city += reservation.net_income * COMMISSION_RATES[form.cleaned_data['city']] / 100

models.py:

from django.db import models


class Reservation(models.Model):
    reservation_code = models.CharField(max_length=150,unique=True)
    checkin = models.DateField()
    checkout = models.DateField()
    flat = models.CharField(max_length=150)
    city = models.CharField(max_length=150)
    net_income = models.DecimalField(max_digits=10, decimal_places=2)

    def __str__(self):
        return self.reservation_code

constants.py:

COMMISSION_RATES = {
    'LONDON': 10,
    'PARIS': 12,
    'PORTO': 9,
}

How to make a query but using only Django ORM? So, there are 3 queries that work, but the idea is to make them all Django ORM.

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

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

发布评论

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

评论(1

︶葆Ⅱㄣ 2025-01-28 10:40:22

您可以尝试这样的事情。 小心,我的代码可能包含错误,这是解决问题的唯一方法。

reservations_table = Reservation.objects.filter(
    city__in=COMMISSION_RATES.keys(),
).annotate(
    instance_commission=F('net_income') * COMMISSION_RATES[F('city')] / Value(100),
).aggregate(
    total_commission=Sum('instance_commission')
)

You can try something like this. Be careful, my code may contain errors, this is only way to solve your problem.

reservations_table = Reservation.objects.filter(
    city__in=COMMISSION_RATES.keys(),
).annotate(
    instance_commission=F('net_income') * COMMISSION_RATES[F('city')] / Value(100),
).aggregate(
    total_commission=Sum('instance_commission')
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文