> I need to retrieve latest record (each record has a time stamp,
> created_at), only one record, from sale table where product_id=1.  How
> do I do this in Django ORM?  I have looked at .objects.extra{}, but I
> am getting ProgrammingError 1064.
>
> I need to get this sql into Django ORM:
>
> select created_at
>       , amount
>       , ...
>   from sales
>   where product_id = 1
> order
>     by created_at desc
>  limit 1

It's very simple - use your normal QuerySet syntax to filter the
product_id and then just say .latest(). See the djando models
documetnation on how to specify on which field(s) should .latest work.

It'll look like:
ModelName.objects.filter(product=product_id).latest();

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to