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.
--
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...@googlegroups.com.
To post to this group, send email to django...@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.
class LlamaHerd(models.Model):
shepherd = ForeignKey(User)
class Llama(models.Model):
size = FloatField()
dob =
FloatField
herd = ForeignKey(Herd, related_name="llamas")
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMGywB4V3zpbqr7v1kcvOi8%2BoqLPm0nr18dGStaedys494%2BOag%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAB%2BAj0vE%2Bc8feae_j8v5_U9UHod1RaphytQQASXD1Dwq4RBGDw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMGywB4mV1%3DFBeECvwOYP3o-ksm_KNN4s6NX7TpCbVCv70h%2BzQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAB%2BAj0tjx%3Dvf3R5T-COATEXF%2BchmUNXiP1vN0HWiMzC7Ws-aYQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMGywB7P1r4UsjZOn%2B8_CdKCY-x2OPsTHySjQpFT0ciUuZGUZw%40mail.gmail.com.