Django Models Unchained.

Jishnu C K
1 min readMar 9, 2022

When you have data that should only be viewed by certain users, you need to have a filtering mechanism that selects only those particular rows in your database table that can be channeled to the front-end.

Here we enhance the Django models.Manager class to further our querying capabilities of the database table. The generic “filter” method is fairly limited when it comes to implementing more complex logic, in this case to display data to a user only if he has the necessary permissions to view it.

models.py

The method get_viewable_data takes the user, filters the table, and limit it to rows that the specific user has access to.

views.py

In views.py which powers the front-end in Django, we can easily get the user info from the request object and pass it to our filtering method , and thus restrict the user to the data he has permission to.

--

--