Help needed in constructing __in query

27 views
Skip to first unread message

Subodh Nijsure

unread,
Aug 5, 2014, 1:11:50 AM8/5/14
to django...@googlegroups.com
I have two tables as shown below.

class AlertTable(models.Model):
id = models.AutoField(primary_key=True)
alarm_text = models.CharField(max_length=800,blank=False,null=False)
event_type = models.SmallIntegerField(blank=False,null=False)
created = models.DateTimeField()
class Meta:
ordering = ['-id']
def __unicode__(self):
return str(self.id)

class PendingAlertsTable(models.Model):
id = models.AutoField(primary_key=True)
alerttable = models.ForeignKey(AlertTable)
created = models.DateTimeField()
class Meta:
ordering = ['-id']
def __unicode__(self):
return str(self.id)

Idea is PendingAlertsTable holds reference to alerts that have not
been acknowledged. And AlertTable holds all the alerts.

So I want to construct query such that I can get all entries from
AlertTable that are Pending.

So I constructed query like this:

inner_qs=PendingAlertsTable.objects.all()
all_pending_events = AlertTable.objects.filter(pk__in=inner_qs)

That doesn't give me query set in all_pending_event of expected
entries. What is the correct way to construct the __in query in this
case?

-Subodh

Collin Anderson

unread,
Aug 5, 2014, 11:00:12 AM8/5/14
to django...@googlegroups.com
AlertTable.objects.filter(pendingalertstable__in=inner_qs)

Or I you could even do:
AlertTable.objects.filter(pendingalertstable__id__gt=0)

Reply all
Reply to author
Forward
0 new messages