Yes, absolutely - my bad. I have updated to SO question. I meant the models to look like:
class LlamaHerd(models.Model): shepherd = ForeignKey(User) class Llama(models.Model): size = FloatField() dob = FloatField herd = ForeignKey(Herd, related_name="llamas")
On Tue, Apr 1, 2014 at 6:09 PM, Bill Freeman <ke1g.nh@gmail.com> wrote:
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 <justin@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+unsubscribe@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.
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+unsubscribe@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.
--
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+unsubscribe@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/CAMGywB6r1wu0hsTVH8JdpBMSWDLBWsH87%2B1H_vZ%2BDqr0Eu%2BSXw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment