2

I am using Django query to filter out some transactions from table where one transaction might have multiple entries in the table.

E.g. Sample table

+---------------+---------+
| TransactionId | Status  |
+---------------+---------+
| Txn0          | Pending |
| Txn0          | Success |
| Txn1          | Fail    |
| Txn2          | Pending |
| Txn3          | Fail    |
| Txn4          | Pending |
| Txn4          | Fail    |
| Txn5          | Pending |
+---------------+---------+

Current query :

SampleTable.objects.exclude(status='Fail').exclude(status='Success')

My current query returns Txn0, Txn2,Txn4, Txn5( because these are marked pending).

I need a queryset which return only row Txn2, Txn5( because all other transaction has atleast one Fail or Success transaction).

Also, tried using .distinct() but that didn't help.

NoobEditor
  • 15,563
  • 19
  • 81
  • 112
kartikmaji
  • 946
  • 7
  • 22
  • Can you add details about how you tried to use `distinct`? It seems like exactly what you need. – MrName Apr 04 '18 at 17:20
  • [something on similar lines...](https://stackoverflow.com/questions/46842758/filter-dataset-not-having-a-value) – NoobEditor Apr 04 '18 at 17:59

2 Answers2

2

Well, not a straight one. But, first filter all the transactions(row) with Failed/success status.

success_fail_txns = SampleTable.objects.filter(status='Success') 
                       | SampleTable.objects.filter(status='Fail')
pending_txns = SampleTable.objects.exclude(txnid__in=success_fail_txns)
kartikmaji
  • 946
  • 7
  • 22
1

you can use exclude with in condition

SampleTable.objects.exclude(status__in=['Fail','Success'])
Robert
  • 3,373
  • 1
  • 18
  • 34