Search code examples
djangofilterdjango-queryset

Django get pk row if pk is found in queryset filter


I have the current table example : enter image description here

I know how to get the rows where the ID is found in :

result_query_1 = models_contacts.objects.filter(company_members_id__icontains= str(pk)+";")

result_query_2 = (here I would like the row of the ID used for the previous query if a match is found)

But I would like to go one step further and also get the row of the ID in another queryset (depending on the first one matches).

For example :

If id 1 is found in company_members_id :

result_query_1 = row N°2 (addidas) + row N°4 (Jordan)

and

result_query_2 = row N°1 (Nike)

I was thinking of using a for loop but I don't Know how to do this in Django.

Maybe there is a simpler / better way to do this ?

Thanks


Solution

  • This is the answer thanks to Igor Moraru in the comments :

     result_query_2 = models_contacts.objects.filter(id=pk).values() if result_query_1.exists() else None