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

4
  • There are two major ways: using fixtures or data migrations. Commented Jan 13, 2020 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') Commented Jan 13, 2020 at 19:33
  • so you mean like piping the command into the django shell? Commented Jan 13, 2020 at 19:51
  • @funnydman how would you do it using migrations? (Just answer so I can have a look) Commented Jan 13, 2020 at 19:51

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.