Basically i want to query for test logs which are Open and Under Process and exclude the Close ones
My Test Model has Status field which is foreign key to BugStatus model as below :
class InspectorTestLog(models.Model):
expected_result = models.TextField(max_length = 1000, blank = True)
status = models.ForeignKey(BugStatus, blank = True , null = True)
datetime = models.DateTimeField(auto_now_add = True)
class BugStatus(models.Model):
status = models.CharField(max_length = 50)status
description = models.TextField(max_length = 1000, blank = True, null = True)
def __unicode__(self):
return self.status
I have given three status as ‘Open’ , ‘Closed’and ‘Under Process’
How do i query for InspectorTestLog objects with status as (‘Open’ and ‘Under Process’) and exclude the ‘Close’ ones
You can use Q objects to combine queries. In you case your query might look as follows:
This will retrieve all test_logs with a status of ‘Open’ or ‘Under Process’ (and by default exclude those with a status of ‘Closed’.)
You could also write a query that just excludes test_logs with a ‘Closed’ status, which would accomplish the same thing: