How can I populate a django database using a script file? - django

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

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

Related

is there any way can i use the Django for existing database? if yes how to use models?

I am working on already existing data on relational database. Now question is that how to build models and how to update the tables with new data coming from user (technically django forms)?
Django natively supports creating models for and working with existing data. From the documentation:
Integrating Django with a legacy database
Django will still need to create several of its own tables, but will adapt to use your existing tables. From the doc, you can auto-create models like this:
python manage.py inspectdb > models.py
You'll need to determine whether you want to manage updates to the table structure, but that's getting into details that will be specific to your project.

Converting postgresql indexes to Django migrations

We have a Django application that has been in production since Django 1.1. Over the years, we've manually added bells and whistles to the production PostgreSQL db that weren't at the time overtly supported by Django's db automation, especially in the form of custom indexes.
Django's come a long way since 1.1, and now w/ 1.10 I'm pretty sure the migration framework supports all the custom features we've added manually. Is there any automated tool which will compare a database to the models, and generate migrations to bring the models up to date with the db?
This is built into django, you can just run inspectdb
manage.py inspectdb
Introspects the database tables in the database pointed-to by the NAME setting and outputs a Django model module (a models.py file) to standard output. You may choose what tables to inspect by passing their names as arguments.
Use this if you have a legacy database with which you’d like to use Django. The script will inspect the database and create a model for each table within it.

How to install custom SQL for unmanaged model in Django

A simple question, is there a way Django can install custom sql for model which is marked as unmanaged ?
I have defined the custom sql file and placed it in the required directory as django documentation stands:
Django provides a hook for passing the database arbitrary SQL that’s executed just after the CREATE TABLE statements when you run migrate. You can use this hook to populate default records, or you could also create SQL functions, views, triggers, etc.
The hook is simple: Django just looks for a file called sql/"modelname".sql, in your app directory, where "modelname" is the model’s name in lowercase.
src: https://docs.djangoproject.com/en/1.8/howto/initial-data/#providing-initial-sql-data
The problem is that it does not work when the model is marked with "managed = False".
As far as I am concerned the django just append the CREATE TABLE statement with the content from custom sql file. Which explain the behavior as the CREATE statement is not present.
But is there any way Django can execute my custom sql ?
My unmanaged model is a model for database view, which I try to create with this custom sql. And I want Django to run this sql automatically while running "manage.py migrate".
I am not interested in putting the sql code inside the migration file as it is quite long and I want to keep in in the sql file.
I am using Django 1.8.
If you name your file correctly, it will be executed each time you run manage.py syncdb.
However, the naming of the file is a little tricky, and unclear from the documentation. For example, if you have an unmanaged model called MyModel in an app called MyApp running on MySQL, then you should have a file located in myapp/sql/mymodel.mysql.sql.
Also, if that file contains a SQL view, the view must be named with a format like myapp_mymodel.

Use Models with a moving/different Sqlite databases?

I am looking to have a django app that visually displays a SQLite database. The idea is that the user would pass in the path to the sqlite file in the URL for the app. I was wondering if I could make use of the Django models and use something like django-tables2 to display it. I was just wondering if the Models are not setup in models.py prior to the start of the server.
Is there a way to still use Model, read/write to the given database and be able to still build the table?

Programatically populate sample data for Django Image/File fields?

I need to populate sample data for Django Image/File fields in automated python code.
I want to initialize Django ImageFields and FileFields with some sample images and data files that are stored in my "site_media" directory of my Django project.
What would the code look like? This is not for testing, I want to autopopulate sample data into my Django website user's accounts (including this sample Imgae/File media.)
This should be done in python code without using fixtures.
If I understand you correctly you want to use fixtures, basically a JSON formatted file that holds data that will be put into the project database. They can be executed through the django-admin command like :
django-admin.py loaddata mydata.json
see http://docs.djangoproject.com/en/1.2/ref/django-admin/#loaddata-fixture-fixture