0👍
✅
try this :
from django.db.models import Q
nv1menus = SettingMenus.objects.filter(
cat__company__name=company,
cat__application="common",
index=0,
role__in=request.user.groups.all()
).exclude(
~Q(sub_categories__role__in=request.user.groups.all())
).distinct()
1👍
You filter with a Prefetch
object [Django-doc]:
from django.db.models import Prefetch
nv1menus = (
SettingMenus.objects.filter(
cat__company__name=company,
cat__application="common",
index=0,
role__user=request.user,
)
.prefetch_related(
Prefetch(
'sub_categories', SettingMenus.objects.filter(role__user=request.user)
)
)
.distinct()
)
this will however not recurse, so it will only filter one level.
Source:stackexchange.com