0

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

Moe Far
  • 2,742
  • 2
  • 23
  • 41
Xavier C.
  • 1,921
  • 15
  • 22

1 Answers1

0

Your code looks fine. The only problem is that you are printing qcm_list. what's that?

Mehdi Pourfar
  • 488
  • 4
  • 13
  • I mean question_list, I made an error when creating the post, but my code is correct on that point. – Xavier C. Feb 12 '16 at 21:31
  • I don't understand what are you trying to do. Why you drop your db after migration? – Mehdi Pourfar Feb 13 '16 at 21:24
  • You are totally right, my error comes from the Makefile, in fact make load-fixtures = python manage.py loaddata data_dump.json. So obviously, no data before that, and that's why I got an empty list... Then I need to correct the makefile and work on the data_dump ! Thanks for your help. – Xavier C. Feb 14 '16 at 17:49