我试图找出为什么Django在migrate
之后尝试makemigrations
时会返回此错误。我尝试更改相关名称并删除上次迁移但没有任何作用。
也许你看到了什么问题?
迁移后出错:django.core.exceptions.FieldDoesNotExist: LanguageQuizSitting has no field named 'questions'
的 Models.py
from django.db import models
from SolutionsForLanguagesApp import models as sfl_models
from jsonfield import JSONField
import random
class LanguageQuiz(models.Model):
name = models.CharField(max_length=40)
language = models.OneToOneField(sfl_models.Language)
class Question(models.Model):
language_quiz = models.ForeignKey(LanguageQuiz,related_name='questions')
text = models.TextField()
class Answer(models.Model):
question = models.ForeignKey(Question,related_name='answers')
text = models.TextField()
correct = models.BooleanField()
class LanguageQuizSitting(models.Model):
user = models.OneToOneField(sfl_models.User)
quiz = models.OneToOneField(LanguageQuiz)
date_opened = models.DateTimeField(auto_created=True)
date_closed = models.DateTimeField(null=True)
max_questions = models.IntegerField()
def load_questions(self):
questions = random.sample(self.quiz.questions,self.max_questions)
for question in questions:
SittingQuestion.objects.create(question=question,sitting=self)
def get_next_question(self):
q = self.questions.filter(answered=0)
if len(q)>0:
return q[0]
else:
return False
class SittingQuestion(models.Model):
sitting = models.ForeignKey(LanguageQuizSitting,related_name='questions',on_delete=models.CASCADE)
question = models.ForeignKey(Question, related_name='SittingQuestions')
answered = models.BooleanField(default=0)
result = models.BooleanField(default=0)
最后一次移民
class Migration(migrations.Migration):
dependencies = [
('language_tests', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='question',
name='language_quiz',
field=models.ForeignKey(related_name='questions', to='language_tests.LanguageQuiz'),
),
migrations.AlterField(
model_name='sittingquestion',
name='question',
field=models.ForeignKey(related_name='sitting_questions', to='language_tests.Question'),
),
migrations.AlterField(
model_name='sittingquestion',
name='sitting',
field=models.ForeignKey(related_name='questions', to='language_tests.LanguageQuizSitting'),
),
]
TRACEBACK:
(venv) C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2>python manage.py migrate
Operations to perform:
Synchronize unmigrated apps: essay, smart_selects, true_false, super_inlines, staticfiles, multichoice, messages, nested_inline, quiz, django_extensions, django_tables2, constance, crispy_forms
Apply all migrations: language_tests, database, admin, sessions, auth, contenttypes, SolutionsForLanguagesApp
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying language_tests.0002_auto_20160711_1524...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\management\__init__.py", line 354, in execute_from_command_line
utility.execute()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\management\__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\management\base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\management\base.py", line 445, in execute
output = self.handle(*args, **options)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\management\commands\migrate.py", line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\migrations\executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\migrations\executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\migrations\migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\migrations\operations\fields.py", line 201, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\backends\base\schema.py", line 484, in alter_field
old_db_params, new_db_params, strict)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\backends\sqlite3\schema.py", line 203, in _alter_field
self._remake_table(model, alter_fields=[(old_field, new_field)])
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\backends\sqlite3\schema.py", line 140, in _remake_table
self.create_model(temp_model)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\backends\base\schema.py", line 236, in create_model
definition, extra_params = self.column_sql(model, field)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\backends\base\schema.py", line 135, in column_sql
db_params = field.db_parameters(connection=self.connection)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\models\fields\related.py", line 2010, in db_parameters
return {"type": self.db_type(connection), "check": []}
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\models\fields\related.py", line 2001, in db_type
rel_field = self.related_field
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\models\fields\related.py", line 1904, in related_field
return self.foreign_related_fields[0]
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\models\fields\related.py", line 1638, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\models\fields\related.py", line 1625, in related_fields
self._related_fields = self.resolve_related_fields()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\models\fields\related.py", line 1618, in resolve_related_fields
else self.rel.to._meta.get_field(to_field_name))
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\db\models\options.py", line 554, in get_field
raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: LanguageQuizSitting has no field named 'questions'
答案 0 :(得分:1)
所以问题如下:
questions = random.sample(self.quiz.questions,self.max_questions)
self.quiz去那里:
quiz = models.OneToOneField(LanguageQuiz)
和quiz.questions去那里:
class LanguageQuiz(models.Model):
name = models.CharField(max_length=40)
language = models.OneToOneField(sfl_models.Language)
问题是LanguageQuiz中不存在的问题。