﻿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
21410	"Error when trying to ignore reverse relationships with related_name using the ""+"""	Troy Grosfield	loic84	"The setup and failing test can be seen below.  It's trying to use the related name as an actual field.  I didn't see this issue until I just upgraded to django 1.6 yesterday.  Am I missing something here?


{{{
# models.py

from django.db import models
from django.contrib.auth.models import User

class ModelA(models.Model):
    created_user = models.ForeignKey(
                    User,
                    related_name='%(app_label)s_%(class)s_created_user+')
    last_modified_user = models.ForeignKey(
                    User,
                    related_name='%(app_label)s_%(class)s_last_modified_user+')
}}}


{{{
# tests.py

from tests.models import ModelA
from django.contrib.auth.models import User


class Django16PrefetchBug(TestCase):

    def test_prefetch_bug(self):
        u1 = User.objects.get_or_create(username='hello1',
                                        email='hello@world.com',
                                        password='hi')[0]
        u2 = User.objects.get_or_create(username='hello2',
                                        email='hello@world.com',
                                        password='hi')[0]
        u3 = User.objects.get_or_create(username='hello3',
                                        email='hello@world.com',
                                        password='hi')[0]

        m1 = ModelA.objects.create(created_user=u1,
                                   last_modified_user=u2)
        m2 = ModelA.objects.create(created_user=u2,
                                   last_modified_user=u3)
        m3 = ModelA.objects.create(created_user=u3,
                                   last_modified_user=u2)
        x = ModelA.objects.all().prefetch_related('created_user')
        # The line below fails when evaluating the query.
        y = list(x)
}}}

{{{
======================================================================
ERROR: test_prefetch_bug (tests.prefetch_bug.Django16PrefetchBug)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/Users/troy/github/django-recurrence/tests/prefetch_bug.py"", line 249, in test_prefetch_bug
    y = list(x)
  File ""/path/to/site-packages/django/db/models/query.py"", line 96, in __iter__
    self._fetch_all()
  File ""/path/to/site-packages/django/db/models/query.py"", line 856, in _fetch_all
    self._prefetch_related_objects()
  File ""/path/to/site-packages/django/db/models/query.py"", line 517, in _prefetch_related_objects
    prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
  File ""/path/to/site-packages/django/db/models/query.py"", line 1598, in prefetch_related_objects
    obj_list, additional_prl = prefetch_one_level(obj_list, prefetcher, attr)
  File ""/path/to/site-packages/django/db/models/query.py"", line 1697, in prefetch_one_level
    prefetcher.get_prefetch_queryset(instances)
  File ""/path/to/site-packages/django/db/models/fields/related.py"", line 277, in get_prefetch_queryset
    qs = self.get_queryset(instance=instances[0]).filter(**query)
  File ""/path/to/site-packages/django/db/models/query.py"", line 590, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File ""/path/to/site-packages/django/db/models/query.py"", line 608, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File ""/path/to/site-packages/django/db/models/sql/query.py"", line 1198, in add_q
    clause = self._add_q(where_part, used_aliases)
  File ""/path/to/site-packages/django/db/models/sql/query.py"", line 1232, in _add_q
    current_negated=current_negated)
  File ""/path/to/site-packages/django/db/models/sql/query.py"", line 1100, in build_filter
    allow_explicit_fk=True)
  File ""/path/to/site-packages/django/db/models/sql/query.py"", line 1351, in setup_joins
    names, opts, allow_many, allow_explicit_fk)
  File ""/path/to/site-packages/django/db/models/sql/query.py"", line 1274, in names_to_path
    ""Choices are: %s"" % (name, "", "".join(available)))
FieldError: Cannot resolve keyword u'tests_modela_created_user+' into field. Choices are: date_joined, email, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, password, user_permissions, username

}}}
"	Bug	closed	Database layer (models, ORM)	1.6	Release blocker	fixed	related_name,		Ready for checkin	1	0	0	0	0	0
