Skip to content
Advertisement

Django ORM Left Join onto same table

I have a custom permissions system in a django project that can link any user to any specific model instance to grant permission on that object. It is more complex than this, but boiled down:

I want to query for all permissions for any user, that are linked to the same content_object(s) that a particular user has been granted permission(s) on. Phrased differently, the user in question wants to see who else has permissions on any of the same objects that they do.

In SQL, the following does the trick:

Is it possible to achieve this in the django ORM?

Advertisement

Answer

You can work with an Exists subquery [Django-doc] which is likely what you intend to do, so:

another option could be to use a “one-two trick” with the ContentType model and work with:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement