Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django Many to Many relationship Query Filter

class servers(models.Model):
hostname=models.CharField(max_length=100)
ip= models.CharField(max_length=100)
os= models.CharField(max_length=100)

class application(models.Model)
name=models.CharField(max_length=100)
URL= models.CharField(max_length=100)
servers= models.ManyToManyField(servers, blank = True, null=True)

current DB status

3 servers 2 with os as linux and 1 with os as windows

2 applications

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Requirement : application can have many servers and each server can be part of many applications also
Need support to create filter only those applications whose os is windows. I tried below but it is returning all three servers.

def viewapp(request,pk)
criterion1 = Q(id=pk)
criterion2 = Q(servers__os__startswith="windows")
prodlist = application.objects.filter(criterion1 & criterion2)  

>Solution :

You can filter the many-to-many field as well with a Prefetch object [Django-doc]:

from django.db.models import Prefetch

def viewapp(request,pk):
    prodlist = application.objects.filter(
        id=pk, servers__os__startswith='windows'
    ).prefetch_related(
        Prefetch('servers', Server.objects.filter(os__startswith='windows'))
    )

since you here work with one object however, it is better to work with:

from django.shortcuts import get_object_or_404
from django.db.models import Prefetch

def viewapp(request, pk):
    prod = get_object_or_404(
        application.objects.prefetch_related(
            Prefetch('servers', Server.objects.filter(os__startswith='windows')
        ),
        pk=pk
    )

Here prod is a single application object, not a QuerySet of applications.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading