﻿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
22865	Testing framework: ValueError: Cannot create form field for 'user' yet, because its related model 'myauth.User' has not been loaded yet	Jon Dufresne	nobody	"All tested with Django 1.6.5

Minimal test case: https://github.com/jdufresne/django-test-value-error

When running unit tests, the test runner does not correctly load all necessary models. This behavior does not occur when running the actual site; tests only.

I have created a minimal test case to illustrate the issue. My hunch is the key points are:

* Do not use contrib.auth, instead create a fully custom user model
* Create an app ""myauth"" to house the custom user model
* Create an app ""myapp"" for other models
* Do not import the actual User model in any file
* Create a different model MyModel with a FK to settings.AUTH_USER_MODEL
* Create a model form for MyModel that inclues the FK as a field
* Create a unit test that imports the form

When running the unit tests, receive the following:

{{{
$ python manage.py test
Creating test database for alias 'default'...
E
======================================================================
ERROR: myapp.tests (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: myapp.tests
Traceback (most recent call last):
  File ""/usr/lib64/python2.7/unittest/loader.py"", line 252, in _find_tests
    module = self._get_module_from_name(name)
  File ""/usr/lib64/python2.7/unittest/loader.py"", line 230, in _get_module_from_name
    __import__(name)
  File ""/home/jon/djtest/djtest/myapp/tests.py"", line 2, in <module>
    from myapp.forms import MyForm
  File ""/home/jon/djtest/djtest/myapp/forms.py"", line 5, in <module>
    class MyForm(forms.ModelForm):
  File ""/home/jon/djtest/venv/lib/python2.7/site-packages/django/forms/models.py"", line 282, in __new__
    opts.help_texts, opts.error_messages)
  File ""/home/jon/djtest/venv/lib/python2.7/site-packages/django/forms/models.py"", line 201, in fields_for_model
    formfield = f.formfield(**kwargs)
  File ""/home/jon/djtest/venv/lib/python2.7/site-packages/django/db/models/fields/related.py"", line 1260, in formfield
    (self.name, self.rel.to))
ValueError: Cannot create form field for 'user' yet, because its related model 'myauth.User' has not been loaded yet


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
Destroying test database for alias 'default'...
}}}

Key files:

settings.py

{{{
...

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
    'myauth',
)

...

AUTH_USER_MODEL = 'myauth.User'
}}}

myauth/models.py

{{{
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import AbstractBaseUser


class User(AbstractBaseUser):
    first_name = models.CharField(max_length=255, db_index=True)
    middle_name = models.CharField(max_length=64, blank=True)
    last_name = models.CharField(max_length=255, db_index=True)
    email = models.EmailField(max_length=255, unique=True)
    is_active = models.BooleanField(default=True)
    is_superuser = models.BooleanField(default=False)
    date_joined = models.DateTimeField(default=timezone.now)

    USERNAME_FIELD = 'email'
}}}

myapp/models.py

{{{
from django.db import models
from django.conf import settings


class MyModel(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)

}}}

myapp/forms.py

{{{
from django import forms
from myapp.models import MyModel


class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ['user']
}}}

myapp/tests.py

{{{
from django.test import TestCase
from myapp.forms import MyForm


class MyFormTestCase(TestCase):
    def test_simple(self):
        form = MyForm()
        self.assertTrue(form)

}}}"	Bug	closed	Testing framework	1.6	Normal	fixed			Unreviewed	0	0	0	0	0	0
