Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install custom SQL with Django

Tags:

python

sql

django

What's the best way to deploy custom SQL for views used by unmanaged Django models?

I have a model myapp.models.MyModel using a view myview.sql, specified in the models "db_table" meta attribute. So, following the docs, I placed the custom SQL file in myapp/sql/myview.sql.

However, after I run python manage.py syncdb, the view is not installed. Running sqlcustom similarly does not show the view.

What else do I need to do so Django will automatically detect and deploy my custom SQL?

like image 940
Cerin Avatar asked Mar 26 '12 02:03

Cerin


2 Answers

python manage.py sqlcustom will only output the custom SQL statements that you've defined, but you need to execute them in the database. Of course, you can copy and paste, but if you want to automate the process and have it be less tedious/error-prone, one easy way to do so (at least on a *nix environment) is to use a pipe:

python manage.py sqlcustom myapp | python manage.py dbshell

like image 112
sacha Avatar answered Sep 24 '22 11:09

sacha


According to the doc, name the file myapp/sql/mymodel.sql instead of myapp/sql/myview.sql

like image 38
okm Avatar answered Sep 21 '22 11:09

okm