Aren't you missing a ForeignKey relationship to tell which LlamaHerd a
Llama belongs to?

Then you would use the reverse relation manager in LlamaHerd to build up
your annotation.


On Tue, Apr 1, 2014 at 5:52 PM, Justin Holmes <jus...@justinholmes.com>wrote:

> I have come across this problem in several Django projects, but only just
> now worked it out in my head enough to make it a cognizable question.
>
> I have also posted it on StackOverflow, here:
> http://stackoverflow.com/questions/22797497/using-the-django-orm-to-order-by-a-value-in-a-distinct-related-item
>
>
> Consider this example:
>
> class LlamaHerd(models.Model):
>     shepherd = ForeignKey(User)
>
> class Llama(models.Model):
>     size = FloatField()
>     dob = FloatField
>
> How can I now make a query to get LlamaHerd objects, sorted by the date of
> birth of their largest llama?
>
> It almost feels like I might be able to use annotate:
>
> LlamaHerd.annotate(largest_llama=Max('llama__size')).order_by('largest_llama__dob')
>
> ...but the annotate here creates fields with float values, not with the
> model instances.
>
>
> --
> Justin Holmes
> Chief Chocobo Breeder, slashRoot
>
> slashRoot: Coffee House and Tech Dojo
> New Paltz, NY 12561
> 845.633.8330
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMGywB6gkBvhFs%2BP2PrtDah24VDCko8QK-1UCnSGugz_ts1v8g%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAMGywB6gkBvhFs%2BP2PrtDah24VDCko8QK-1UCnSGugz_ts1v8g%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAB%2BAj0srKNzRs%3DzbsPm%2BobxLNFCcjRccGSVeYykF1PSnJc-ydQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to