Django filter value not in a list

I want to ask how can I filter table object that is not in a list of values.

For example this is my list

kyc_cat_list = ['Urgent Cases', 'NSB - Alimony Wallet', 'SGP - Site Guard Wallet', 'Suspended Cases - Rejected by BDC']

I want to get all records that are not in the above list

records = history_table.objects.filter(KYC_Category not in kyc_cat_list)

My Table:

class history_table(models.Model):
id=models.AutoField(primary_key=True)
Dial = models.IntegerField(null=False)
Serial = models.IntegerField(null=False, default=0)
Bag = models.IntegerField(null=False, default=0)
wave_no = models.CharField(max_length=50,null=False, blank=False)
type = models.CharField(max_length=100, null=False, blank=False)
Today_Date = models.DateField(auto_now_add=True,null=True, blank=True)
Today_Date_Time = models.DateTimeField(auto_now_add=True,null=True, blank=True)
user = models.CharField(max_length=100)
summary = models.CharField(max_length=100,null=False, blank=False)
Name = models.CharField(max_length=100,null=True, blank=True)
Customer_Address = models.CharField(max_length=100,null=False, blank=False)
National_ID = models.IntegerField(null=True, blank=True)
Customer_ID = models.IntegerField(null=True, blank=True)
Status = models.CharField(max_length=100,null=True, blank=True)
Registration_Date = models.DateTimeField(max_length=100,null=True, blank=True)
Is_Documented = models.CharField(max_length=100,null=False, blank=False)
Needed_Action = models.CharField(max_length=100, null=False, blank=False)
Hard_Copy_Type = models.CharField(max_length=100, null=False, blank=False)
Request_Status = models.CharField(max_length=100, null=False, blank=False)
KYC_Category = models.CharField(max_length=100, null=False, blank=False)

>Solution :

Use exclude, not filter.

latest_record = history_table.objects.exclude(KYC_Category__in=kyc_cat_list)

Leave a Reply