Can I use a database view as a model in Django?

I would like to use the view that I created in my database as a source for my django-view.

Is this possible without using custom sql?

****** 13/02/09 UPDATE ***********

Like many of the suggested answers, you can simply create your own view in the database and then use it in the API by specifying it in models.py.

some warning:

  • manage.py syncdb will no longer work
  • to view you need the same thing at the beginning of your name as all other models (tables), for example, if your application is called a “thing”, then your view will need to be called thing_ $ viewname
+55
django django-migrations django-syncdb
Feb 03 '09 at 16:17
source share
5 answers

Starting with Django 1.1, you can use Options.managed .

For older versions, you can easily define the model class for the view and use it like your other views. I just tested it with a Sqlite based application and it seems to work fine. Just remember to add the primary key field if the primary key column of your view does not have the name id and indicates the name of the view in the Meta options if your view is not called app_classname.

The only problem is that the syncdb command will throw an exception as Django will try to create the table. You can prevent this by specifying "view models" in a separate Python file other than models.py. Thus, Django will not see them during the introspection of models.py to determine the models created for the application, and therefore will not try to create a table.

+33
Feb 03 '09 at 17:13
source share

Just an update for those who come across this issue (from Google or everything else) ...

Django currently has a simple “right way” to define a model without managing database tables :

Options.managed

The default is True , which means that Django will create the appropriate database tables in syncdb and delete them as part of the reset control command. That is, Django manages the life cycles of database tables.

If False , no operations will be created for this model to create or delete a database table. This is useful if the model is an existing table or database view that was created in other ways. This is the only difference when managed is False . All other aspects of model processing are exactly the same as normal.

+90
Aug 15 '09 at 3:22
source share

I just implemented a model using a view with postgres 9.4 and django 1.8.

I created custom migration classes as follows:

 # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import migrations class Migration(migrations.Migration): dependencies = [ ('myapp', '0002_previousdependency'), ] sql = """ create VIEW myapp_myview as select your view here """ operations = [ migrations.RunSQL("drop view if exists myapp_myview;"), migrations.RunSQL(sql) ] 

I wrote the model as usual. This works for my purposes.

Note When I started makemigrations, a new migration file was created for the model, which I manually deleted.

Full disclosure - my view is read only because I use the view obtained from the jsonb data type and did not write the ON UPDATE INSTEAD rule.

+9
Apr 09 '15 at 7:10
source share

We did this quite extensively in our MySQL applications to work with the only limitation of the Django database. Our application has a couple of databases running in a single instance of MySQL. We can make the cross-database model integrate this way while we create the views for each table in the “current” database.

As for the insert / update in the views, in our use cases, the view is basically "select * from [db.table];". In other words, we don't do any complex joins or filtering, so the insert / updates trigger from save () works just fine. If your use case requires such complex joins or advanced filtering, I suspect that you will not have problems with read-only scripts, but there may be a problem with the insert / update. I think that in MySQL there are some basic restrictions that prevent updating views that intersect tables, have complex filters, etc.

In any case, your mileage may change if you use a DBMS other than MySQL, but Django does not care if it sits on top of a physical table or view. It will be an RDBMS that determines whether it really works as you expect. As the previous commentator noted, you are most likely to throw syncdb out of the window, although we successfully worked on it using the post-syncdb signal, which removes the physical table created by Django and runs our "create view ..." command. However, the signal after synchronization is a bit esoteric in the sense in which it is triggered, so also caution emptor.

EDIT: Of course, “postsynchronous signal” means “postsynchronous listener”

+3
Feb 07 '09 at 2:56
source share

From the Django Official Documentation, you can call the following:

 #import library from django.db import connection #Create the cursor cursor = connection.cursor() #Write the SQL code sql_string = 'SELECT * FROM myview' #Execute the SQL cursor.execute(sql_string) result = cursor.fetchall() 

Hope this helps; -)

+2
Sep 23 '13 at 12:47
source share



All Articles