0

I started programming with django. Now I have to import some data from files. What is the best way to do this in django 1.7?

I just only want to do this once, so I wrote a script, where I can put the data into the database of django. My problem at this point is, that I have to check some dependencies, before I add data to database.

I tried to check, if the Value, what I want to import, already exist in Database. But I get Error:

{AppRegistryNotReady}Models aren't loaded yes.

So I think I do something wrong.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
knumskull
  • 186
  • 1
  • 2
  • 8
  • https://docs.djangoproject.com/en/dev/releases/1.7/#schema-migrations - You can no longer us data fixtures. (check the 5th point) – karthikr Nov 16 '14 at 15:04

1 Answers1

0

I believe this is happening because the script needs to be run within the context of your Django application and using your settings file.

This answer shows a good example of doing this for Django 1.7+ Runtime error:App registry isn't ready yet

Or for pre Django 1.7, put this at the top of your script and edit paths accordingly...

import sys, os
sys.path.append('/path/to/your/django/app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from django.conf import settings

Another option would be to create this as a management command so that it could be executed via manage.py which sets the correct Django context for you. https://docs.djangoproject.com/en/dev/howto/custom-management-commands/

Community
  • 1
  • 1
Kevin Cherepski
  • 1,473
  • 8
  • 8
  • I tried the first option, but there I run into the error I mentioned above. I followed your hint with the command and have to say. Cool. That's what I want. This is the right solution to do some initial stuff. Thanks! – knumskull Nov 16 '14 at 16:52