12

I'm trying to populate my Django database with a script so I don't have to enter in the data manually. So far I have the following:

from my_app.models import ModelInApp
import django

django.setup()


def add_data(data1, data2):
    d, created = ModelInApp.objects.get_or_create(data1=data1, data2=data2)
    print("- Data: {0}, Created: {1}".format(str(d), str(created)))
    return d


def populate():
    # data is a list of lists
    for row in data:
        data1 = row[0]
        data2 = row[1]
        add_data(data1, data2)


if __name__ == "__main__":
    populate()

The data variable is a list of lists containing the data I want to put into my database. The problem is, when I run the script, I get a django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. error. I am using PostgreSQL as the database.

What am I doing wrong?

Evan Bloemer
  • 1,051
  • 2
  • 11
  • 31

4 Answers4

17

I know it's been like forever but...

Although not technically a script, Django allows database seeding by creating a fixture

The documentation is very intuitive. This is always my go-to method when pre-populating a database in Django. For example, if you have a model Cars:

class Cars(models.Model):
    make = models.CharField(max_length=50)
    color = models.CharField(max_length=50)

You can create a seeding fixture cars.json like:

[
  {
    "model":"myapp.cars",
    "pk":1,
    "fields":{
      "make": "Audi",
      "color":"Black"
    }
  },
  {
    "model":"myapp.cars",
    "pk":2,
    "fields":{
      "make": "Aston Martin",
      "color":"Blue"
    }
  }
]

To prepopulate the DB, just run ./manage.py loaddata cars.json

I find this is the best option if you for example have to load data like all the car models or all countries and their flags in the same lines for every web app you create...

Dave Mackey
  • 4,306
  • 21
  • 78
  • 136
karuoro
  • 541
  • 4
  • 12
12

The fast way to work it out is to populate your DB from Django shell (python manage.py shell). Your code without import django and django.setup() should work fine. Fast & dirty ;)

In case you need to add data from time to time, consider writing a management command. This will allow you to call python manage.py my_command_name datafile.dat. You can implement whatever logic inside the command (data from file or API, create or update).

Another way to add data is to use data migration: https://simpleisbetterthancomplex.com/tutorial/2017/09/26/how-to-create-django-data-migrations.html

Also, consider using bulk_create to make things more efficient (filter the data list before to avoid duplicates)

nad_rom
  • 419
  • 4
  • 9
  • What if I want to create a dashboard web app for internal use? I don't want to manually update all the data that can be retrieved via scripts within the python shell.. I would want the data to be automatically added whenever someone views the URL.. Is this possible with django? – Llewellyn Hattingh Apr 07 '20 at 03:32
  • You will still have to update the data in your Django database. A good way is to write a custom management command to update the data and run it whenever you are having an update (cron job?) https://docs.djangoproject.com/en/3.0/howto/custom-management-commands/ . When the database is fine, your dashboard will always display updated data – nad_rom Apr 09 '20 at 11:06
7

I had a similar problem when running a population script, i.e. django core exception. I solved the problem by adding the code

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','my_django_project.settings')

import django
django.setup()

Here my_django_project.settings points to the Python package for your project. If you are following the introductory tutorial Writing your first Django app, it should read mysite.settings.

The code you show in your example, should read something like shown below, although you have to specify names as indicated by question marks.

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','???.settings')

import django
django.setup()

from ???.models import ModelInApp

def add_data(data1, data2):
    d, created = ModelInApp.objects.get_or_create(data1=data1, data2=data2)
    print("- Data: {0}, Created: {1}".format(str(d), str(created)))
   return d


def populate():
    # data is a list of lists
    for row in data:
        data1 = row[0]
        data2 = row[1]
        add_data(data1, data2)


if __name__ == "__main__":
    populate()
John
  • 1,645
  • 2
  • 17
  • 29
0

I had the same error my fix was to move this:

from my_app.models import ModelInApp

after the

django.setup()

in the main function

example:

if __name__ == '__main__':
    django.setup()
    # import AFTER setup
    from MyApp.models import ModelInApp
    from django.contrib.auth.models import User
KillerKingTR
  • 9
  • 1
  • 3