ORM Question

48 views
Skip to first unread message

Justin Holmes

unread,
Apr 1, 2014, 5:52:29 PM4/1/14
to django...@googlegroups.com

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

Bill Freeman

unread,
Apr 1, 2014, 6:09:38 PM4/1/14
to django-users
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.


--
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.

Justin Holmes

unread,
Apr 1, 2014, 6:13:07 PM4/1/14
to django...@googlegroups.com
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")




For more options, visit https://groups.google.com/d/optout.

Justin Holmes

unread,
Apr 1, 2014, 6:20:28 PM4/1/14
to django...@googlegroups.com
You say "use the reverse relation manager in LlamaHerd to build up your annotation," but that answer the question.

How can I sort LlamaHerd objects by the DOB of their largest llama?

Bill Freeman

unread,
Apr 2, 2014, 10:17:11 AM4/2/14
to django-users
1.  You now have a foreign key to Herd, but you don't have a model by that name.  It's called LlamaHerd.

2.  I was misinterpreting what you wanted (couldn't get past the lack of a relationship while reading0.  Once the relationship is fixed, your original annotate might work.


Justin Holmes

unread,
Apr 2, 2014, 12:04:24 PM4/2/14
to django...@googlegroups.com
No no, the annotate doesn't work - that's the whole reason for my question.  See, the annotate doesn't associate instances with the annotated attribute, it associated results.

(BTW, good looking out - I have once again fixed the SO question :-))



For more options, visit https://groups.google.com/d/optout.

Bill Freeman

unread,
Apr 2, 2014, 12:35:40 PM4/2/14
to django-users
My point was that annotate might work after the other problems were fixed.  Have you re-tested since?  If your annotate still doesn't work then perhaps someone with more annotate experience than I will comment.



Justin Holmes

unread,
Apr 2, 2014, 9:31:23 PM4/2/14
to django...@googlegroups.com
I think you misunderstand: I am not actually tracking llama herds. :-)

This is just example code; I haven't actually tested it in the way that you suggest.  The problem isn't with this code in particular, it's with this concept in general.  This is a question I have had again and again in the past few years - this code is just a way to articulate it.

So, in short - no, the code won't work as now written, and it's obvious why: annotate doesn't assign instances, it assigns results.

I don't think I need an annotation expert per se; it may well be that annotate is the wrong way to do this.

My SQL isn't particularly strong, but if I were writing this in SQL, I suspect that GROUP_BY is necessary or at least plausible.  I know that Django doesn't directly expose GROUP_BY, but it looks likes QuerySet.values() uses it; so I wonder if that fits into the picture somehow?



For more options, visit https://groups.google.com/d/optout.

Bill Freeman

unread,
Apr 3, 2014, 11:44:09 AM4/3/14
to django-users
From the lack of other folks jumping in saying "do it this way", I'm going to guess that this is a hard problem.  I am not a database heavyweight.  (IANADbH)  I especially don't know about limitations of MySQL.

It seems that you need to calculate a maximum size of Llama objects, but separately for each LlamaHerd, and then you have to select, again for each LlamaHerd, choose a Llama of hte corresponding size (what if there are multiple animals in that herd with that size?) to include in the result, and then attach an order by date of birth to the final result.

I don't know how to do that in one SQL statement.  I do know from experience that it's easy, by choosing a naive approach, to have such an aggregate size of join tables that the database starts to swap.

So lets hope that a DbG (Database Guru) pipes up with an answer.


Reply all
Reply to author
Forward
0 new messages