Search code examples
djangodatabasepython-3.xautomationdjango-2.0

Django 2.0 Populating Database with a Script


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?


Solution

  • 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)