Django explicit `order_by` by ForeignKey field

53 views
Skip to first unread message

alTus

unread,
Apr 25, 2014, 6:50:55 PM4/25/14
to django...@googlegroups.com
Hi. So I have some small models:

class Group(models.Model):
    name = models.CharField()
    
    class Meta:
        ordering = ('name',)

class Entity(models.Model):
    name = models.CharField()
    group = models.ForeignKey(Group, null=True)

Now I want to perform a really simple query like this:

SELECT * FROM `app_entity`
WHERE `app_entity`.`name` = 'something'
ORDER_BY `app_entity`.`group_id` ASC;

Note that I really want to order by the field itself with no useless joins or smth else. So I write smth like that:

Entity.objects.filter(name='something').order_by('group')

and django happily makes this query:

SELECT `app_entity`.`id` FROM `app_entity`
LEFT OUTER JOIN `app_group` ON ( `app_entity`.`group_id` = `app_group`.`id` )
WHERE `app_entity`.`name` = 'smth' 
ORDER BY `app_group`.`name` ASC

According to the docs it uses default Group model ordering and this default behaviour makes sense.
And I need this ordering to be set (for admin and some other places).
If I don't set ordering param in Group.Meta - I get what I want: explicit query shown above (1st one).
But as I said I need it. And I just can't get this query easily.
So we have some sort of inconsistant behaviour - I can do the right thing without ordering param and I can't do it with it.

order_by('group_id') doesn't work and raises FieldError.
For now I'm using extra(order_by=['app_entity.group_id']) or even order_by('app_entity.group_id') but it's quite ugly and it's still such a trivial thing that should have a simple and straight ORM solution.

As I think this might be considered as a bug or smth. In django sourses I found some place where field names lile `group_id` work.
For example, `values_list` method. But this behaviour is marked as a hack there.

So the first question is whether do I miss smth and it can be done somehow.
And the second, if not, do I need to submit a ticket for this.

Simon Charette

unread,
Apr 25, 2014, 8:46:37 PM4/25/14
to django...@googlegroups.com
The issue was referenced in #19195 and it was suggested we add the ability to order by field name and not only by attribute name.

I suggest you open a ticket for this and add a reference to it in #19195.

In the mean time you can get the correct ordering by issuing a order_by('group__pk') but you'll have to live with the extraneous JOIN.

Thanks,
Simon

Simon Charette

unread,
Apr 26, 2014, 1:33:49 AM4/26/14
to django...@googlegroups.com
Actually the FieldError is not raised anymore in Django 1.7 (and master) and issuing a .order_by('group_id') is the equivalent of .order_by('group') which respect related model ordering if defined.

I would argue we should seize the opportunity to provide a way of opting out of this default behavior while maintaining backward compatibility by shipping this in Django 1.7.

I'm afraid that if we wait until 1.7 is released with this *fix* we'll have to provide an extra api to .order_by (say follow=False) to fix this correctly.

We should move this discussion forward to django-developers in order to get feedback from the community.


Simon

Le vendredi 25 avril 2014 18:50:55 UTC-4, alTus a écrit :

alTus

unread,
Apr 26, 2014, 7:38:48 AM4/26/14
to django...@googlegroups.com
Thanks! Yes, this sounds logical. Haven't found anywhere here how to move theme to another "forum". Is this possible? Or I should just copy it?

суббота, 26 апреля 2014 г., 9:33:49 UTC+4 пользователь Simon Charette написал:
Reply all
Reply to author
Forward
0 new messages