Django and Oracle database (accessing your views via ORM)

2

Hello, despite the self explanatory title. I'll detail my doubt below.

I have a pre-existing Oracle database, I integrated it with django by configuring the default connection in settings and running manage.py inspectdb > _models.py. So creating a file with several mapped classes from my database.

My question is: This database has views, predefined sql routines that return a very useful data selection. I would like to know how to use them within django because they are not mapped natively and my lay understanding of database and orm leave me with many choices other than trying to rewrite their logics in pure sql.

Excuse me if the answer is simple and thanks for the amparo in advance.

    
asked by anonymous 24.05.2017 / 04:58

1 answer

2

I would like to thank Jefferson Quesado openly for sharing a link with the ideal logic, having just had to adapt to my development environment.

Actually, DataBase Views are common database tables, with their dynamically generated values during your query, they work as select roaming, and do not take up space in the database, even though they are persistent. My low understanding of this has hurt the creative pursuit of a solution.

The detail really came from the fact that they are persistent, they are then considered real database tables and therefore can also be mapped.

I created a class in my _models.py with the exact name of my View of the Oracle database in question, following the standard scope of a class that represents a relational object. I inherited it from models.Model , added its attributes with the exact names and properties of the fields of the view I wanted to map (if you have questions about which properties to assign, take a peek at your table's SQL and see the properties of the fields it references , including a very important detail, the primary / foreign keys for the model of your view are actually the keys of the tables that your view abstracts, even if it seems unnecessary, I would like to leave this detail with due emphasis here, since in the scope of the If you do not want to create a django extension, you will not be able to see any of the django objects in the list. p>

Finally, the class primary_key=True within of the template class of your base date view. Referencing your view as a real table (after all it's in the gut is).

class Meta:
    managed = False
    db_table = 'nome_da_sua_view'

On the plus side, I highly recommend visiting the link left by Jefferson Quesado, Worth checking out.

    
25.05.2017 / 01:34