Django初学者:如何在django ORM中查询以根据日期计算字段
任务:在 Django ORM 中查询模型,以便我可以根据日期计算字段。 具体来说,从 model.datefield 中提取月份并根据这些月份计算值。
示例模型:
class PersonProjectHours(models.Model):
project = models.ForeignKey('projects.Project')
person = models.ForeignKey('projects.Person')
rate = models.ForeignKey('PersonIncome')
work_date = models.DateField(help_text=_('Enter date'))
hours = models.IntegerField(help_text=_('Enter hours worked on this day.'))
class PersonIncome(models.Model):
person = models.ForeignKey('projects.Person')
income = models.DecimalField(help_text=_('Enter new income'), max_digits=10, decimal_places=2)
validdate = models.DateField(help_text=_('Enter date from which new income is valid.'))
在我的views.py中,我可以像这样提取每月工作的月份和小时数(我使用一个范围,因为我无法弄清楚如何在ORM中以月份为单位查询月份)。我可以通过循环每个月的条目来计算参与该项目的不同人员的工作时间成本(现在不起作用,因为entry.rate是一个unicode,我无法隐藏它到一个整数...):
for month in range(1, 13):
entries_per_month = PersonProjectHours.objects.filter(work_date__month=month)
hours = entries_per_month.aggregate(value=Sum('hours'))
cost = 0
for entry in entries_per_month:
cost = cost + (entry.hours * entry.rate)
work_per_year.append([month,hours,cost])
为了完成这个例子,我循环遍历模板中的条目,如下所示:
{% for month, hours, cost in work_per_year %}
<tr>
<td>{{ month }}</td>
<td>{{ hours.value }}</td>
<td>{{ cost }}</td>
</tr>
{% endfor %}
我在views.py中所做的事情看起来不太优雅,是否有更好的方法来提取日期范围,例如日期字段中的年、月或日?顺便问一下,我怎样才能让entry.rate成为一个我可以计算的整数?
感谢您的投入! (是的,我对编码、Python 和 django 非常陌生......我花了一周的时间来写这篇文章):-)
Task: Querying a model in Django ORM so that I can calculate fields based on dates.
Specifically, extract months from a model.datefield and calculate values based on these months.
Example model:
class PersonProjectHours(models.Model):
project = models.ForeignKey('projects.Project')
person = models.ForeignKey('projects.Person')
rate = models.ForeignKey('PersonIncome')
work_date = models.DateField(help_text=_('Enter date'))
hours = models.IntegerField(help_text=_('Enter hours worked on this day.'))
class PersonIncome(models.Model):
person = models.ForeignKey('projects.Person')
income = models.DecimalField(help_text=_('Enter new income'), max_digits=10, decimal_places=2)
validdate = models.DateField(help_text=_('Enter date from which new income is valid.'))
In my views.py, I can extract the months and hours worked per month like this (I use a range because I couldn't figure out how to query for month in months in ORM). And I can calculate the cost of the hours worked by the different people who worked on the project by looping through the entries in each month (just doesn't work right now because entry.rate is a unicode and I somehow can't covert it to an integer...):
for month in range(1, 13):
entries_per_month = PersonProjectHours.objects.filter(work_date__month=month)
hours = entries_per_month.aggregate(value=Sum('hours'))
cost = 0
for entry in entries_per_month:
cost = cost + (entry.hours * entry.rate)
work_per_year.append([month,hours,cost])
Just to complete this example, I loop through the entries in my templates like this:
{% for month, hours, cost in work_per_year %}
<tr>
<td>{{ month }}</td>
<td>{{ hours.value }}</td>
<td>{{ cost }}</td>
</tr>
{% endfor %}
What I have done in views.py doesn't seem very elegant, is there a better way to pull date ranges like years, months or days from datefields? And on the sidelines, how do I get entry.rate to be an integer I can calculate?
Thanks for your input! (yes, I am very new to coding, python, and django... took me a week to write this up) :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我最终根据上述输入和其他 stackoverflow 帖子开发的解决方案。
Here's the solution that I ultimately developed with the above input and other stackoverflow posts.
你的日期范围拉动对我来说似乎很好。
Entry.rate 是模型 PersonIncome 的外键。将小时数乘以 PersonIncome 实例的 pk 是没有意义的。
我会将费率字段添加到人员:
然后您可以在视图中执行此操作:
而这假设人员对于完成的任何类型的工作始终具有相同的费率。
华泰
Your date range pulling seems fine for me.
entry.rate is a foreign key to the model PersonIncome. Would make no sense to multiply the hours with the pk of the PersonIncome instance.
I would add the rate field to the Person:
Then you can do this in the view:
Whereas this assumes that a Person has always the same rate for any kind of work done.
HTH