﻿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
25786	set_*_order raises ValueError when ForeignKey referenced Model also has OneToOneField	aktiur	Tim Graham	"Hello everyone,

I came across a bug where trying to order models with respect to their ForeignKey raises a ValueError when the ForeignKey referenced model has a OneToOneField to another completely unrelated model.

Here is a stripped down version of my models that still triggers the bug

{{{
#!div style=""font-size: 80%""
Code highlighting:
  {{{#!python
  from django.db import models
 
 
  class Entity(models.Model):
      pass


  class Dimension(models.Model):
      entity = models.OneToOneField('Entity', primary_key=True)


  class Component(models.Model):
      dimension = models.ForeignKey(""Dimension"")

      class Meta:
          order_with_respect_to = ""dimension""
  }}}
}}}

And here is the triggering code :
{{{
#!div style=""font-size: 80%""
Code highlighting:
  {{{#!python
  >>> e = Entity.objects.create
  >>> d = Dimension.objects.create(entity=e)
  >>> c1 = d.dimension_set.create()
  >>> c2 = d.dimension_set.create()
  >>> d.set_dimension_order([c1.id, c2.id])
  }}}
}}}

It raises :
{{{
#!div style=""font-size: 80%""
Exception:
  {{{
  Traceback (most recent call last):
   File ""<input>"", line 1, in <module>
   File ""C:\Miniconda3\envs\anap-mesis\lib\site-packages\django\utils\functional.py"", line 17, in _curried
     return _curried_func(*(args + moreargs), **dict(kwargs, **morekwargs))
   File ""C:\Miniconda3\envs\anap-mesis\lib\site-packages\django\db\models\base.py"", line 1683, in method_set_order
     ordered_obj.objects.filter(**{'pk': j, order_name: rel_val}).update(_order=i)
   File ""C:\Miniconda3\envs\anap-mesis\lib\site-packages\django\db\models\manager.py"", line 127, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
   File ""C:\Miniconda3\envs\anap-mesis\lib\site-packages\django\db\models\query.py"", line 679, in filter
     return self._filter_or_exclude(False, *args, **kwargs)
   File ""C:\Miniconda3\envs\anap-mesis\lib\site-packages\django\db\models\query.py"", line 697, in _filter_or_exclude
     clone.query.add_q(Q(*args, **kwargs))
   File ""C:\Miniconda3\envs\anap-mesis\lib\site-packages\django\db\models\sql\query.py"", line 1309, in add_q
     clause, require_inner = self._add_q(where_part, self.used_aliases)
   File ""C:\Miniconda3\envs\anap-mesis\lib\site-packages\django\db\models\sql\query.py"", line 1337, in _add_q
     allow_joins=allow_joins, split_subq=split_subq,
   File ""C:\Miniconda3\envs\anap-mesis\lib\site-packages\django\db\models\sql\query.py"", line 1178, in build_filter
     self.check_related_objects(field, value, opts)
   File ""C:\Miniconda3\envs\anap-mesis\lib\site-packages\django\db\models\sql\query.py"", line 1073, in check_related_objects
     self.check_query_object_type(value, opts)
   File ""C:\Miniconda3\envs\anap-mesis\lib\site-packages\django\db\models\sql\query.py"", line 1057, in check_query_object_type
     (value, opts.object_name))
 ValueError: Cannot query ""Entity object"": Must be ""Dimension"" instance
  }}}
}}}

I'm using Python 3.4.3 / Django 1.8.4.

I'm not very familiar with django's internals, but it seems to me that the bug happens in django.db.models.base, in method_set_order, on line 1677.
More precisely, I have noticed that :
{{{
#!div style=""font-size: 80%""
Code excerpt:
  {{{
  >>> m.Component._meta.order_with_respect_to.rel
  <ManyToOneRel: order.component>
  >>> m.Component._meta.order_with_respect_to.rel.field_name
  'entity'
  }}}
}}}"	Bug	closed	Database layer (models, ORM)	1.8	Release blocker	fixed	order_with_respect_to ForeignKey OneToOneField		Accepted	1	0	0	0	0	0
