Django初学者:如何在django ORM中查询基于日期计算字段 [英] Django beginner: How to query in django ORM to calculate fields based on dates

查看:242
本文介绍了Django初学者:如何在django ORM中查询基于日期计算字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务:在Django ORM中查询模型,以便我可以根据日期计算字段。
具体来说,从model.datefield提取月份,并根据这些月份计算值。



示例模型:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 Person')
rate = models.ForeignKey('PersonIncome')
work_date = models.DateField(help_text = _('Enter date'))
hours = models.IntegerField(help_text = _ ('在这一天工作的小时')


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 = _('输入新收入有效的日期。))

在我的views.py中,我可以提取每月工作的月份和时间,如这(我使用范围b因为我不知道如何在ORM中查询几个月的月份)。我可以通过循环访问每个月的条目来计算工作在不同的工作人员工作时间的成本(现在不行,因为entry.rate是一个unicode,我以某种方式无法隐藏它)到一个整数...):

 范围内的月份(1,13):
entries_per_month = PersonProjectHours。 objects.filter(work_date__month = month)
hours = entries_per_month.aggregate(value = Sum('hours'))
cost = 0
在entries_per_month中输入:
cost = cost +(entry.hours * entry.rate)
work_per_year.append([month,hours,cost])

只是为了完成这个例子,我循环访问我的模板中的条目,如下所示:

  {%for month ,小时,成本在work_per_year%} 
< tr>
< td> {{month}}< / td>
< td> {{hours.value}}< / td>
< td> {{cost}}< / td>
< / tr>
{%endfor%}

我在views.py中做的不似非常优雅,是否有更好的方式从日期范围拉出日期范围,如数月,数月或数天?而且在边线上,如何使entry.rate成为一个可以计算的整数?



感谢您的输入! (是的,我非常新的编码,python和django ...花了我一个星期写这个): - )

解决方案

您的日期范围拉动对我来说似乎很好。
entry.rate是模型PersonIncome的外键。将小时数与PersonIncome实例的pk相乘是没有意义的。



我将添加速率字段给Person:

  class Person(models.Model):
name = models.CharField(max_length = 100)
rate = models.IntegerField(max_length = 4 )

然后,您可以在视图中执行此操作:

  cost = cost +(entry.hours * entry.person.rate)

虽然这假设一个人对于完成的任何工作总是相同的费率。
HTH


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) :-)

解决方案

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:

class Person(models.Model):  
    name = models.CharField(max_length=100)  
    rate = models.IntegerField(max_length=4)  

Then you can do this in the view:

cost = cost + (entry.hours * entry.person.rate)

Whereas this assumes that a Person has always the same rate for any kind of work done. HTH

这篇关于Django初学者:如何在django ORM中查询基于日期计算字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆