0

I built a small django app and created some models. Now I would like to populate the database (the tables and rows do already exist) using a python script since I have to read the information I want to populate the database with, from multiple external files. Is there any way to do that?

EDIT: Python 3.7 Django 3.0

byTreneib
  • 176
  • 13
  • There are two major ways: using fixtures or data migrations. – funnydman Jan 13 '20 at 19:09
  • I am new into Django, but I guess the the script file must use the python shell like: % python manage.py shell >>> from products.models import Product >>> Product.objects.all() >>> Product.objects.create(title='new product 2', description='another one', price='19312', summary='sweet') – Ben Link Jan 13 '20 at 19:33
  • so you mean like piping the command into the django shell? – byTreneib Jan 13 '20 at 19:51
  • @funnydman how would you do it using migrations? (Just answer so I can have a look) – byTreneib Jan 13 '20 at 19:51

1 Answers1

1

You can always use any python routine to read files, process the content, create your model instances and save them the to the database by using a custom Django management command.

check this out: how to import csv data into django models

panchicore
  • 11,451
  • 12
  • 74
  • 100