Home > Enterprise >  Find a get queryset values that existes in another queryset of tha same model
Find a get queryset values that existes in another queryset of tha same model

Time:12-09

Iam tring filter permission queryset that exists in another Queryset of permission. How can filter that Queryset

####### My code

staff_perm = instance.permissions.all().
designation_perms = instance.designation.permissions.all()

# needed_designation_perms = designation_perms that exists in staff_perm

Ex : Consider A and B are Querysets

A = [1,2,3,4,5]
B = [1,3,5,7,9]

i want 

C = [1, 3, 5]
here C values from 'A' and values from 'B that exists in A'

CodePudding user response:

It would be possible if you could iterate it through the for loop:

for x in desig_permission:
    if x in staff_permission:
        #DO SOMETHING like add to a list
    else:
        pass

CodePudding user response:

If the expected values are unique, then you can try this(compare across sets):

C = list(set(A).intersection(set(B)))
  • Related