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?
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 much 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 car models or all countries and their flags or sth in the same lines for every web app you create...