I've already started this topic in django-users (
https://groups.google.com/forum/#!topic/django-users/k9K7VsPe6aA) and got 
an advice there to ask here.
It's not a big issue and it can be solved quite easily as I can see from 
django sources. Also there're some workarounds to deal with it but I write 
here coz I thinks that it makes django behaviour inconsistent.

So we have 2 models:

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

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

Now I want to perform a really simple query like this (to order by group_id 
field itself):
SELECT * FROM `app_entity` ORDER_BY `app_entity`.`group_id` ASC;

So we use smth like that:
Entity.objects.order_by('group')

and get (not a big surprise, it's well documented behaviour):
SELECT * FROM `app_entity`
LEFT OUTER JOIN `app_group` ON ( `app_entity`.`group_id` = `app_group`.`id` 
)
ORDER BY `app_group`.`name` ASC

The only way to get rid of JOIN is to remove ordering attribute from Group 
Meta class. But it's not a solution coz it's used in many other places.
So the inconsistency is that we have 2 different behaviours with and 
without ordering attribute in related model.
When we don't have ordering defined we can easily order by the field itself 
as well as order by any field in related model if we need to.
While when it's defined we loose the way to order by the field itself 
(still can use extra or even explicitly specify table name but it's all 
quite ugly).

Quite obvious solution is to use `order_by('group_id')` for explicit field 
ordering.
But in django<1.7 trying to order by `order_id` raises FieldError. In 1.7 
order_by('group_id') became identical to order_by('group').
So if 1.7 is released with that new behaviour it will become 
backwards-incompatible to use `order_by('group_id')` for this purpose.

Related topic: #19195 <https://code.djangoproject.com/ticket/19195>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/59fa8a5e-7dc0-49f1-a123-6d654efb7d00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to