dimanche 1 octobre 2017

ValueError: Related model u'app.model' cannot be resolved when running tests

I am trying to run some tests on a Django project (Django version 1.11.4) using Python 3.5.0.

I have 2 applications inside my project: uploads and testgen.

Those are my models:

(Here there are only the special fields, like the relationships fields. The rest of the fields are CharField, PositiveIntegerField and BooleanField mainly.)

uploads\models.py

(a simplified version)

class Document(models.Model):
    (any relationship fields)


class Word(models.Model):
    synonyms = models.ManyToManyField("self")
    antonyms = models.ManyToManyField("self")


class Sentence(models.Model):
    words = models.ManyToManyField(Word)


class Paragraph(models.Model):
    sentences = models.ManyToManyField(Sentence)


class Text(models.Model):
    document = models.ForeignKey(Document, on_delete=models.CASCADE)
    paragraphs = models.ManyToManyField(Paragraph)

testgen\models.py

(a simplified version)

class Issue(models.Model):
    content = models.OneToOneField(Sentence,
                                   related_name="issue_content",
                                   null=True)
    question = models.OneToOneField(Sentence, null=True)


class FillableIssue(Issue):
    replaceable_words = models.ManyToManyField(Word)


class StatementIssue(Issue):
    replaceable_words = models.ManyToManyField(Word)


class AppTest(models.Model):
    text = models.ForeignKey(Text, null=True)
    fillable_issues = models.ManyToManyField(FillableIssue)
    statement_issues = models.ManyToManyField(StatementIssue)

testgen\tests.py

from django.test import TestCase
from testgen.models import AppTest


class AppTestTestCase(TestCase):

    def test_apptest_has_positive_number_issues(self):

        """
            has_positive_number_issues() returns True
            if the test's number issues is greater than zero.
        """

        app_tests = AppTest.objects.get_queryset().all()
        for app_test in app_tests:
            self.assertIs(app_test.has_positive_number_issues(), True)

project settings file:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'uploads',
'testgen',
]

Note: I use the uploads models inside the testgen models for the application logic.

I suspect that the model's name could be the problem ('AppTest')

Traceback output:

(env_01) C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\funquizz>python manage.py test testgen
Creating test database for alias 'default'...
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\core\management\__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\core\management\commands\test.py", line 29, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\core\management\base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\core\management\commands\test.py", line 62, in handle
    failures = test_runner.run_tests(test_labels)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\test\runner.py", line 601, in run_tests
    old_config = self.setup_databases()
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\test\runner.py", line 546, in setup_databases
    self.parallel, **kwargs
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\test\utils.py", line 187, in setup_databases
    serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\backends\base\creation.py", line 69, in create_test_db
    run_syncdb=True,
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\core\management\__init__.py", line 130, in call_command
    return command.execute(*args, **defaults)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\core\management\base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\migrations\operations\fields.py", line 87, in database_forwards
    field,
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\backends\sqlite3\schema.py", line 238, in add_field
    self._remake_table(model, create_field=field)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\backends\sqlite3\schema.py", line 198, in _remake_table
    self.create_model(temp_model)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\backends\base\schema.py", line 254, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\backends\base\schema.py", line 144, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\models\fields\related.py", line 994, in db_parameters
    return {"type": self.db_type(connection), "check": self.db_check(connection)}
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\models\fields\related.py", line 991, in db_type
    return self.target_field.rel_db_type(connection=connection)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\models\fields\related.py", line 909, in target_field
    return self.foreign_related_fields[0]
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\models\fields\related.py", line 653, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\models\fields\related.py", line 640, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "C:\Users\Asus\Documents\material_itis\pfc\funquizz_webapp\env_01\lib\site-packages\django\db\models\fields\related.py", line 625, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'testgen.AppTest' cannot be resolved

The traceback can also be inspected in the screenshots images.

first capture second capture

Aucun commentaire:

Enregistrer un commentaire