On 5/5/2009 5:13 AM, Alfonso wrote:
> I'm using the following to pull all cost prices that match a
> particular product by the company that supplied the product:
>
> View:
>
> cost_prices = ProductCostPrice.objects.filter
> (product_id__product_id=product_id)
>
> Template:
> ...
> {% regroup cost_prices|dictsort:"supplier" by supplier as cost_list %}
> {% for cost in cost_list %}
> {% for item in cost.list %}
>
> {{ item.date_cost|date:"d/m/Y" }}
> {{ item.supplier }}
> {{ item.cost_price_gross }}
>
> {% endfor %}
> {% endfor %}
>
> Works great - only thing is that in some cases there is more than one
> cost price in the db for a supplier, how do I just grab the latest
> price entered for each supplier? Familiar with latest() lookup but
> not sure about it's use in the above scenario. I feel I should know
> this but can't make it work. FYI there is normally more than one
> supplier for a product hence why it's set up like this.
You might be able to solve this with some fancy SQL. However, the way I
would approach it is to model how you would use this information in your
domain.
Since you have the need to know what the "latest" price is for a given
product/supplier combination, I would add a `ProductCostPrice.is_latest`
boolean field. At `ProductCostPrice.save()` you can use whatever logic
you like to determine what the value of that flag should be for all rows
with that product/supplier combination, ensuring only one record has
is_latest=True. That way it's trivial to filter on that field. And the
data also makes sense directly in the table.
By the way, `filter(product_id__product_id=product_id)` looks funny to
me. Are you sure you don't want just `filter(product__pk=product_id)`?
--
George
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@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
-~--~~~~--~~--~--~---