Django: database access - django

I have a Django project called myproj, with an app called myapp, containing a model called Mymodel. In myproj/myapp, I want to create a Python script that accesses the database. I know how to access the database from myproj/myapp/views.py, but if I have a blank file at myproj/myapp/myscript.py, then what do I need to do to enable database interaction? Currently, if I just write the following in the script:
from models import mymodel
objs = Mymodel.objects.all()
But I get a load of errors for that. Any help?

Create a custom management command

Related

How to import data from csv file to the default Django User model?

I know there is an import-export package which allows us to import data from csv file to our own models by making changes in the admin.py file, but I am not able to do this if I want to import data from csv file to the default User model present in Django. Is there any way to do that?
You most likely want to write your own custom Management Command for this job.
You can import the CSV data easily with Tablib.
In the management command you have complete access to your Django models. Then you just invoke your new command with python manage.py <name_of_your_command>!

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

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

gender as a model from a SQL statement in django 1.8

I want to generate SQL code and take from that code, generating a Django model to avoid errors.
They will say that you first create the model and run the syncdb or migrate but my case is unlike the database is already created and I now want the model
Run this command to auto-generate models from an already existing database. But first make sure you've properly linked database to django app .
python manage.py inspectdb > models.py
Do check models.py file and make some changes if you something isn't rendered correctly.
For inspectdb approach, read this: https://docs.djangoproject.com/en/1.8/howto/legacy-databases/
Alternatively, you can write all the models by yourself and set managed = False. No database table creation of deletion will be executed by Django on this model. But it is somewhat complicated and puts some limits on model relationships.
For managed=False approach, read this: https://docs.djangoproject.com/en/1.8/ref/models/options/#managed

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.

Connecting and importing models from multiple database connections Django

In a django app, I have two postgresql databases connected through settings.py: one is default and other is AppDb. AppDb is placed on a remote machine.
I want to query from a 'Courses' model from AppDb using 'using()' and 'Courses' model is not available in default database.
So my query goes like this:
courseInfo = Courses.objects.using('AppDb').filter(cuser_id = 12)
But I am getting NameError for 'Courses'
Can I have a solution for such queries without using routers
If you have an existing database, you still need to create an app and models for that database in order to use the ORM.
To help you create the model classes, you can use the inspectdb management command which will try to guess the models from an existing database and create the models.py for you. Its not perfect, but it will save you some time.
You will still have to make sure the models have a primary key and are written in the correct order (so that foreign keys will work correctly).