﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
18507	Django 1.3 breaks ordering by the reverse of a one-to-one field	Klaas van Schelven	nobody	"I have a piece of code that breaks on migrating from 1.2 to 1.3. There is no mention of this in the 1.3 release notes, specifically in the section that speaks of backwards incompatibilities.

Basically, I'm poking the order_by value on User's Meta to be ordered by properties of !UserProfile. !UserProfile has a !OneToOneField to user. On Django 1.2 this meant I could use !UserProfile's fields for sorting Users. On Django 1.3 things start to break badly.

I.e.:

{{{
class UserProfile(models.Model):
    user = models.OneToOneField(User, editable=False)
    
    first_name = models.CharField(max_length=255, blank=True, verbose_name=_(""Firstname""))
    name_prefix = models.CharField(max_length=50, blank=True, verbose_name=_(""Lastname prefix""))
    last_name = models.CharField(max_length=255, blank=True, verbose_name=_(""Lastname""))

    ...
    
User._meta.ordering = ('userprofile__last_name', 'userprofile__name_prefix', 'userprofile__first_name')
}}}

Django 1.2 situation:
{{{
>>> from django.contrib.auth.models import User
>>> u = User.objects.all()[0]
>>> User._meta.get_field_by_name('userprofile')
(<RelatedObject: userprofile:userprofile related to user>, None, False, False)
>>> 
}}}


Django 1.3 situation:
{{{
>>> from django.contrib.auth.models import User
>>> u = User.objects.all()[0]
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  [...]
  File ""/home/klaas/dev/online/demo/django/db/models/sql/query.py"", line 1238, in setup_joins
    ""Choices are: %s"" % (name, "", "".join(names)))
FieldError: Cannot resolve keyword 'userprofile' into field. Choices are: ...
>>> User._meta.get_field_by_name('userprofile')
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File "".../django/db/models/options.py"", line 313, in get_field_by_name
    % (self.object_name, name))
FieldDoesNotExist: User has no field named 'userprofile'
}}}

The call to get_field_by_name is what goes wrong in the first place (i.e. it's the method call in setup_joins that's failing), so I've highlighted that above. I assume get_field_by_name() no longer existing for reverse fields might also cause breakage in certain applications, but haven't used it myself in a way that breaks. 

I suppose it's not related to poking on User's Meta, and is a problem in general, but I haven't checked that yet."	Uncategorized	closed	Database layer (models, ORM)	1.3	Normal	worksforme			Unreviewed	0	0	0	0	0	0
