Get all rows where field matches another field in another table in django -


say have table called watchlist, contains list of entities i'm concerned about:

class watchlist(models.model):     entity = models.foreignkey(entity)     objects = watchlistmanager()      def __str__(self):         return str(self.entity) 

and have list of alerts:

class distinctalert(models.model):     alert_type = models.foreignkey(alerttype, db_index=true, on_delete=models.cascade)     entities = models.manytomanyfield(to='entity', db_index=true, through='entitytoalertmap')     has_unattended = models.booleanfield(default=true)     latest_datetime = models.datetimefield() 

disregarding alert type i'm trying distinctalerts long entity exists in watchlist.

something this:

distinctalert.objects.filter(entities__in=watchlist.objects.all()).all() 

but of ocurse doesn't work since require entity objects instead of watchlist objects. what's best approach this? should do:

distinctalert.objects.filter(entities__in=[element.entity element in self.all()]).all() 

not sure if iterating on every element , constructing list outside right way it, or if it's possible pass queryset so:

distinctalert.objects.filter(entities__in=watchlist.objects.all()).all() 

(the above example wouldn't work me since they're watchlist objects , not entity objects)

you can follow backwards relation entity watchlist:

distinctalert.objects.filter(entities__watchlist__isnull=false) 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -