Django Models A basic unmanaged table.


At some point in your use of Django, you may find yourself wanting to interact with tables which have already been created, or with database views. In these cases, you would not want Django to manage the tables through its migrations. To set this up, you need to add only one variable to your model's Meta class: managed = False.

Here is an example of how you might create an unmanaged model to interact with a database view:

class Dummy(models.Model):
    something = models.IntegerField()

    class Meta:
       managed = False

This may be mapped to a view defined in SQL as follows.

CREATE VIEW myapp_dummy AS 
SELECT id, something FROM complicated_table 
WHERE some_complicated_condition = True

Once you have this model created, you can use it as you would any other model:

>>> Dummy.objects.all()
[<Dummy: Dummy object>, <Dummy: Dummy object>, <Dummy: Dummy object>]
>>> Dummy.objects.filter(something=42)
[<Dummy: Dummy object>]