python - Django ORM: Recursive many-to-many field join -


i have these models:

class permission(models.model):     per_id = models.foreignkey("self",null=true)     permissions = models.manytomanyfield("self", null=true)     type = models.integerfield(blank=true, null=true) 

and:

class user(models.model):     name = models.charfield(max_length=50, blank=true)     permissions = models.manytomanyfield(permission) 

we have roles have type=1823 point many permission type=1822 , permission=null (many-to-many recursive relation), both "roles , permissions" store in permission table. want user permission using django orm.

user.objects.get(pk=3).permissions.filter(permissions__isnull=false).values("permission__id") "gives role's permissions  user.objects.get(pk=3).permissions.filter(permissions__isnull=true).values("id") "gives permissions 

i wanna combine , distinct these 2 querysets in single query.

thanks

something solved problem :

permission.objects.filter(q(permission__user__id=3) | q(user__id=3), type=1822).distinct() 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -