#22865: Testing framework: ValueError: Cannot create form field for 'user' yet,
because its related model 'myauth.User' has not been loaded yet
-----------------------------------+--------------------
Reporter: jdufresne | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 1.6
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------
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)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22865>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.6c55c85a1a7d80c7fa06b81569b15ba2%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.