How to install custom SQL for unmanaged model in Django - 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.

Related

Detect database DDL schema changes with Django

Let's say that we have a Django app that looks on a legacy database.
If someone make changes on some database tables from a db client as DBeaver for example and not through Django models, is there a way to identify these changes?
You can do in a terminal, inside your Django project directory : python manage.py inspectdb > models.py
You will have models related to your tables.
By default, inspectdb creates unmanaged models. That is, managed = False in the model’s Meta class tells Django not to manage each table’s creation, modification, and deletion.
If you do want to allow Django to manage the table’s lifecycle, you’ll need to change the managed option above to True (or remove it because True is its default value).

Do I have to use Django Models to use the API?

We are migrating our intranet application and have decided to choose the Django framework. We control all of our database via source control and managed scripts, so we do not use the migrate feature to create tables for us. Views and Tables can change, all business logic is held in the database.
I want to make Django API endpoint which is basically select * from my_table_or_view; and Django can return a JSON response with the column names and values. Some database tables have close to 100 columns each so I don't want to write out each and every field name and type just to return a query. What happens if we add another column to the view - will I have to update it in Django as well? What if I change the column type - will my application fail as well?
The frontend is written in VueJS - it makes a request to the API endpoint and should use the columns selected in the frontend, obviously if I remove a column it will break but I don't want to have to add the column in the django framework even if it is not used.
I've read the raw SQL queries section of the docs but i'm not sure where this applies to. Does this logic sit in the views section?
I've tried directing a URL endpoint in URLS.py to a custom class in views.py but not sure this is correct, does this logic need to be in a serializer?
I'd like the simplest method possible, potentially not using models, just raw SQL is fine.
One option for you is to sync your db to django models every time you change your schema and then use it normally, both ORM and raw SQL queries would be possible.
There's a function called inspectdb that does that natively, here is a reference to it
https://docs.djangoproject.com/en/3.1/ref/django-admin/#inspectdb

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

Prevent FieldDoesNotExist in Django app on production server during migration

I run migration on server in this way:
Upload models.py file to server with some new field sfield im model Mobject
Perform makemigration command in manage.py
perform migrate command in manage.py
But there are some requests between end of first step and end of third step which are failed with django.core.exceptions.FieldDoesNotExist: Mobject has no field named 'sfield' (Which is obvious, becouse django ORM can't fetch this field from DB but field already in Class, so django will try to do it)
Is it possible to make all 3 steps "Atomic"? Or globaly ignore this exceptions, becouse for now I don't need sfield, I only want perform migration without Exceptions. Or may be I can temporarely mark new field in some way to prevent django fetching it from DB, but it must be visible for makemigrations/migrate?
if you do select * from yourtable then django tries to fetch all fields defined in the model.
you can use only() in your orms to select specific fields, so that no exception will be raised while migrating new fields that are not used in orm yet
btw, you should create migration files locally, test the new field on your local machine and then commit the migration files to server. In server while deploying, you then need only migrate right after deployment, which makes the time shorter where exceptions can happen.
from the django docs:
The reason that there are separate commands to make and apply
migrations is because you’ll commit migrations to your version control
system and ship them with your app; they not only make your
development easier, they’re also useable by other developers and in
production.

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