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.

Reply via email to