Roland,

Shooting a bit in the dark but how about:

...
select = {
    'days_in_stock': "'%s' - transactie_datum_inkomend + 1" %
(given_date.isoformat()),
    'avg_days_instock': 'AVG(days_in_stock)',
})
...
qs.annotate(Count('manufacturer'))
...

Would that get you the group by and average field you were looking for?

If not it might be easier to just write what you want as custom SQL.

Ian

On Tue, May 18, 2010 at 10:51 PM, Roland van Laar <rol...@micite.net> wrote:
> On 05/18/2010 03:43 PM, Ian Lewis wrote:
>> Roland,
>>
>> Instead of using annotate, could you just add the avg_days_instock
>> field as another key in the select argument to extra?
>>
>
> Well how would I do that?
> Since I want the avg_days_instock calculated on a 'group_by' basis.
> To me it seems that I need to do a Count and a avg_days_instock at the
> same time?
>
> Roland
>
>> Ian
>>
>> On Tue, May 18, 2010 at 9:03 PM, Roland van Laar <rol...@micite.net> wrote:
>>
>>> Hello,
>>>
>>> Question: How do I calculate the Average of a calculated field.
>>>
>>> My (simplified) models.py:
>>>
>>> Manufacturer(models.Model):
>>>    name = models.CharField(length=100)
>>>
>>> Product(models.Model):
>>>    name = models.CharField(length=100)
>>>
>>> Items(models.Model):
>>>    manufacturer = models.Foreignkey(Manufacturer)
>>>    product = models.ForeignKey(Product)
>>>    bought_date = models.DateField()
>>>    sold_date = models.DateField(null=True, blank=True)
>>>
>>> On these models I want to calculate the number of days an item was in stock
>>> on a given date:
>>>
>>> qs = Items.objects.all().extra(select= \
>>>    {'days_in_stock': "'%s' - transactie_datum_inkomend + 1" %
>>> (given_date.isoformat())})
>>>
>>> And then I want to know the average number of days all the items selected on
>>> manufacturer and product.
>>>
>>> qs.values('manufacturer', 'product)
>>> qs.annotate(Count('manufacturer'), avg_days_instock=Avg('days_in_stock'))
>>>
>>> The annotate fails because 'days_in_stock' is not a valid field:
>>> FieldError: Cannot resolve keyword 'days_in_stock' into field. Choices are:
>>> manufacturer, product, bought_date, sold_date
>>>
>>> How can I calculate the average of the days_in_stock?
>>>
>>> Thanks,
>>>
>>> Roland van Laar
>>>
>>>
>>>
>>> --
>>> 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.
>>>
>>>
>>>
>>
>>
>>
>
> --
> 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.
>
>



-- 
=======================================
株式会社ビープラウド  イアン・ルイス
〒150-0021
東京都渋谷区恵比寿西2-3-2 NSビル6階
email: ianmle...@beproud.jp
TEL:03-6416-9836
FAX:03-6416-9837
http://www.beproud.jp/
=======================================

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