﻿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
19472	AttributeError: Problem installing auth.user fixtures with swapped user model	Chris Wilson	nobody	"If a project swaps out a user model, then fixtures in apps that refer to
auth.user don't work, which breaks their tests. This makes it impossible
to write reusable apps whose tests still work when the user model is
swapped out.

I've created a [https://github.com/qris/django-swapout-user-model-breaks-app-tests test case] which fails like this:

{{{
chris@lap-x201:~/user_swapout_test/project$ ../ve/bin/python manage.py test app_with_user_fixtures
Creating test database for alias 'default'...
E
======================================================================
ERROR: test_basic_addition (app_with_user_fixtures.tests.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/installuser/Dropbox/projects/ischool/user_swapout_test/ve/local/lib/python2.7/site-packages/django/test/testcases.py"", line 256, in __call__
    self._pre_setup()
  File ""/home/installuser/Dropbox/projects/ischool/user_swapout_test/ve/local/lib/python2.7/site-packages/django/test/testcases.py"", line 450, in _pre_setup
    self._fixture_setup()
  File ""/home/installuser/Dropbox/projects/ischool/user_swapout_test/ve/local/lib/python2.7/site-packages/django/test/testcases.py"", line 828, in _fixture_setup
    'skip_validation': True,
  File ""/home/installuser/Dropbox/projects/ischool/user_swapout_test/ve/local/lib/python2.7/site-packages/django/core/management/__init__.py"", line 161, in call_command
    return klass.execute(*args, **defaults)
  File ""/home/installuser/Dropbox/projects/ischool/user_swapout_test/ve/local/lib/python2.7/site-packages/django/core/management/base.py"", line 252, in execute
    output = self.handle(*args, **options)
  File ""/home/installuser/Dropbox/projects/ischool/user_swapout_test/ve/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py"", line 111, in handle
    self.load_label(fixture_label, app_fixtures)
  File ""/home/installuser/Dropbox/projects/ischool/user_swapout_test/ve/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py"", line 194, in load_label
    formats)
  File ""/home/installuser/Dropbox/projects/ischool/user_swapout_test/ve/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py"", line 246, in process_dir
    obj.save(using=self.using)
  File ""/home/installuser/Dropbox/projects/ischool/user_swapout_test/ve/local/lib/python2.7/site-packages/django/core/serializers/base.py"", line 167, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File ""/home/installuser/Dropbox/projects/ischool/user_swapout_test/ve/local/lib/python2.7/site-packages/django/db/models/base.py"", line 604, in save_base
    manager.using(using).filter(pk=pk_val).exists())):
AttributeError: Problem installing fixture '/home/installuser/Dropbox/projects/ischool/user_swapout_test/project/app_with_user_fixtures/fixtures/users.json': 'NoneType' object has no attribute 'using'

----------------------------------------------------------------------
Ran 0 tests in 0.006s

FAILED (errors=1)
Destroying test database for alias 'default'...
}}}

If I add some instrumentation to find out why the manager is None:

{{{
manager = cls._base_manager
if manager is None:
    raise ValueError((""%s must have a valid Manager to create "" +
        ""instances"") % cls)
}}}

Then I can see that it's `django.contrib.auth.models.User` that has no manager:

{{{
ValueError: Problem installing fixture '/home/installuser/Dropbox/projects/ischool/user_swapout_test/project/app_with_user_fixtures/fixtures/users.json': <class 'django.contrib.auth.models.User'> must have a valid Manager to create instances
}}}

I wonder if we need a special case for UserModel in fixtures. Of course if you swap out the user model for one that doesn't contain the necessary fields, then the fixture cannot be loaded, and the test should fail in that case. But when the fields are present, we should be able to load the fixtures.

Perhaps we could special case the content_type `auth.user` and any (future) swappable model class content types, to refer to the swapped-in model instead of the swapped-out one, for backwards compatibility?"	Bug	closed	Documentation	1.5-beta-1	Release blocker	fixed			Accepted	0	0	0	0	0	0
