Using Django 1.8.2, I'm trying to make a migration on one model.
Here is an exemple of the model:
class Question(models.Model):
module = models.ForeignKey(Module)
my_order = models.PositiveIntegerField(default=0, blank=False, null=False)
question = models.CharField(max_length=400)
And here is the migration:
# -*- 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),
]
I've created this based on this documentation : https://docs.djangoproject.com/fr/1.8/topics/migrations/#data-migrations
My problem is that even if there is Question objects in the database, the question_list is always empty. However, printing question_model works fine, so I guess that I've got the right model. Then I don't understand why I can't get the corresponding objects.
Any help would be appreciate,
Thanks,
Xavier
EDIT :
In fact, I've found that my problem might not be in the migration itself, because when I use python manage.py migrate
every thing works fine, but when I use my makefile to reinit-database, the error occurs.
Here is the 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
Then I don't really understand what is the reason it don't work but now I know a trick...
Anyway, if anybody know what is happening, I'm interested !
Xavier
答案 0 :(得分:0)
Your code looks fine. The only problem is that you are printing qcm_list. what's that?