与Django Orm进行查询?
我已经使用此代码进行查询:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试这样的事情。 小心,我的代码可能包含错误,这是解决问题的唯一方法。
You can try something like this. Be careful, my code may contain errors, this is only way to solve your problem.