0

I provide a lista of users for my application in a json located in my app:

myapp/
  fixtures/
    initial_data.json

it load everytime I run python manage.py migrate. I've read the Providing initial data for models document, but it does not mention anything about avoid loading it.

I wonder if there's a command to run python manage.py migrate without load initial_data.

Gocht
  • 9,924
  • 3
  • 42
  • 81

2 Answers2

0

You should use data migration instead, because fixtures are not for use together with migrations. Fixtures are deprecated since Django 1.7 and will be removed in Django 1.9

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
  • Thanks for your answer. [Providing initial SQL data](https://docs.djangoproject.com/en/1.8/howto/initial-data/#providing-initial-sql-data) is deprecated and will be removed. Django's documentation for version 1.8 still recomend to use .json format – Gocht Aug 13 '15 at 15:19
  • .json format is not deprecated, loading initial data fixtures this way is. Move your fixture to migrations folder and create migration that will push data from fixture into database. – GwynBleidD Aug 13 '15 at 15:22
0

Consider upgrade to Django==1.8. You shouldn't have many problems after upgrade. What's your Django version?

Anyway, take a look at the documentation, since Django>=1.7 automatic loading of fixtures doesn't work:

If an application uses migrations, there is no automatic loading of fixtures

An application uses migrations when you have the migrations folder with the __init__.py file inside it.

If you use Django>=1.7 and you use migrations in your app, you don't have automatic fixtures loading when you run python manage.py migrate.

But, if you don't use migrations in your app, you always have automatic fixtures loading if your fixture file is named initial_data.json.

You can rename the initial_data.json file to any other name (like mydata.json) in order to avoid automatic fixtures loading, then, you can load the data any time you want by running:

django-admin.py loaddata mydata.json

maoaiz
  • 165
  • 1
  • 10
  • I'm using Django 1.8.3 and when I run migrate initial_data loads, I want to avoid it sometimes. – Gocht Aug 13 '15 at 17:21
  • Are you sure the fixtures are loaded every time you migrate? Let me see your migrations files. Have you edit them? – maoaiz Aug 13 '15 at 19:25
  • No, I haven't. I just created an app and put a folder named "fixtures" inside and add a initial_data.json file. Now when I run python manage.py migrate, initial_data file data is loaded in db – Gocht Aug 13 '15 at 20:35
  • Create a folder named **migrations** between *fixtures* folder, put a file named **__init__.py** inside it. Then, run: `python manage.py makemigrations` and `python manage.py migrate`. Any time you can view the migrations on your project runing: `python manage.py migrate --list` – maoaiz Aug 13 '15 at 20:45
  • Provided data are loading, and that is fine. But I recently made a change in a model and migrations fails, because the data in json haven't those new fields, that's why I wonder if there's a command to avoid loading initial_data – Gocht Aug 13 '15 at 20:50
  • You can temporarily rename the folder *fixtures* or the *initial_data* file, if there aren't fixtures to load, the migrate command won't load any fixtures. PD: do you have the migrations folder? – maoaiz Aug 13 '15 at 20:58
  • Yes, I have a migrations folder. That's what I did, I needed run the project fast so I didn't any search and I just renamed the folder and ran migrate command, but after that I wonder if it is possible to avoid loading initial data with a command, change the folder's name is not elegant I think. – Gocht Aug 13 '15 at 21:01
  • The answer is not, you can not prevent to load initial data when run migrations if your file is named *initial_data.json*, and of course, rename the folder is not elegant, but rename initial_data.json is a good idea, when you need to load the fixtures you should run `django-admin.py loaddata mydata.json`. You need to fix your migrations files in order to avoid problems. How many migrations files do you have? what shows the `python manage.py migrate --list` command? – maoaiz Aug 13 '15 at 21:17
  • I have 11 files in migrations folder. and `python manage.py migrate --list` shows the list of files. – Gocht Aug 13 '15 at 21:25
  • What do you think about renaming *initial_data* file? – maoaiz Aug 13 '15 at 21:47
  • Yes, looks like that's the best option, rename it and load explicitly. – Gocht Aug 13 '15 at 21:54