0
使用Django 1.8.2,我试图在一个模型上进行迁移。Django迁移,MyModel.objects.get()返回空列表
下面是模型的为例:
class Question(models.Model):
module = models.ForeignKey(Module)
my_order = models.PositiveIntegerField(default=0, blank=False, null=False)
question = models.CharField(max_length=400)
这里是迁移:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
def save_answer(apps, schema_editor):
question_model = apps.get_model("modules", "question")
print question_model
question_list = question_model.objects.all()
print question_list
for question in question_list:
#some changes here
question.save()
class Migration(migrations.Migration):
dependencies = [
('modules', '0002_auto_20160212_1940'),
]
operations = [
migrations.RunPython(save_answer),
]
我已经在此基础上创建的文档这样的:https://docs.djangoproject.com/fr/1.8/topics/migrations/#data-migrations
我的问题是即使数据库中存在Question对象,question_list始终为空。 但是,打印question_model工作正常,所以我想我有合适的模型。 然后我不明白为什么我不能得到相应的对象。
任何帮助将是感激,
感谢,
泽维尔
编辑: 其实,我发现我的问题可能不是迁移本身,因为当我使用python manage.py migrate
每件事情都可以正常工作,但是当我使用我的makefile重新创建数据库时,会发生错误。 这里是Makefile中:
reinit-database:
psql -c "DROP DATABASE myDataBase" || true
psql -c "CREATE DATABASE myDataBase"
find . -type d -name "migrations"
find . -type f -name "*.pyc" -delete
python manage.py reset_db
python manage.py migrate auth || true
python manage.py migrate
make load-fixtures
python manage.py createsuperuser
然后,我真的不明白它是什么不工作的原因,但现在我知道了一招......
无论如何,如果有人知道发生了什么, 我很感兴趣 !
Xavier
我的意思是question_list,我在创建帖子时发生了错误,但是我的代码在该点上是正确的。 –
我不明白你想做什么。为什么在迁移后放弃数据库? –
你是完全正确的,我的错误来自Makefile,实际上是使load-fixtures = python manage.py loaddata data_dump.json。 显然,之前没有数据,这就是为什么我得到一个空列表... 然后,我需要更正生成文件,并在data_dump上工作! 感谢您的帮助。 –