2010/10/14 Jonathan Barratt <jonathan.barr...@knifeict.com>

> On 14 ?.?. 2010, at 22:27, Marc Aymerich wrote:
>
> Hi,
> I'm trying to make a Manager for my model "order". I want that it returns
> all orders that their 'active period'* is greater than a certain threshold.
> With "active period" I mean: (order.cancel_date - order.register_date )
>
> My Order model looks like this:
>
> class order(models.Model):
>     register_date = models.DateTimeField(auto_now_add=True)
>     cancel_date = models.DateTimeField(null=True, blank=True)
>
>
> I'm wondering if is possible to achieve that using "annotate", something
> like that:
> (this not work)
>
> order.objects.annotate(diff=SUB('cancel_date','register_date')).filter(diff__gt=period_threshold)
>
>
> or maybe django provides another kind of tool to achieve what i want ?
>
>
> Unless I'm misunderstanding what you're looking for, I think this might
> suit your needs:
>
> in your model:
>
> class order(models.Model):
>
> ...
>     def get_active_period(self): return self.cancel_date -
> self.register_date active_period = property(get_active_period)
> (you may want to add some checking that cancel_date is not null, or that
> active_period is not negative but you get the idea)
>
> Then where you need to access that queryset use:
>
> order.objects.filter(active_period__gt=period_threshold)
>

Hi Jonathan!,
Thank you very much for your answer, it's a pretty good solution :)



-- 
Marc

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to