Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MySQL view in django?

I would like to use a view that I've created in MySQL as model in Django. I would like to show the status of unfinished processes going on at my company on my intranet site. I have found only rather old entries on forums, so is there an easy way to pass MySQL view through syncdb?

If not, assume I have a two tables:

  1. hard-writen milestones of subprocesses, e.g :
  2. table with processess and subprocesses info as ID from table above : enter image description here

  3. My view in MySQL would do something like that. 4) So the outcome would be :

I would like to have only one model in my django APP, the outcome no.4. What is the quickest way to get this ? Custom SQL query? Or do I need to have models for progress_tb and proc_tb and combine them? How do I refer to the same table few times (as in Access/MySQL I have progress_tb as progress_tb_1 et c.) ? And by now I know only how to make a filtered query, but how can I "add" few columns from joined table ? I tried "extra" option from django, but I don't understand how it works :(

like image 870
Jakub Avatar asked Jun 01 '15 16:06

Jakub


1 Answers

You can specify your view as not being managed by Django through a managed = False field declaration on your nested Meta class:

class MyViewModel(models.Model):
    field1 = models.IntegerField()
    [...]

    class Meta:
        managed = False
        db_table = 'MyView' # your view name

managed = False will cause python manage.py syncdb to ignore that model for sync'ing.

like image 168
Ross Rogers Avatar answered Sep 29 '22 18:09

Ross Rogers