Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django filter queryset if a field exists

I want to filter my queryset if and only if loan exists in my model ShgGroupLoanMarking.

class ShgGroupLoanMarking(models.Model):
 shg = models.ForeignKey(Shg, null=True)
 category = models.CharField(max_length=25, choices=GROUP_CATEGORY, default="shg_group")
 msss = models.ForeignKey(Msss, null=True)
 shgmember = models.ForeignKey(ShgMember, null=True)
 loan = models.ForeignKey(Loan, null=True)
 date_of_marking = models.DateField(null=True)
 paid = models.CharField(max_length=100, null=True)
 loan_amount = models.CharField(max_length=100, null=True)
 service_charge = models.CharField(max_length=100, null=True)
 description = models.CharField(max_length=100, null=True)
 status = models.CharField(max_length=100, null=True)
 print_status = models.BooleanField(default=False)
 sent_to_bank_status = models.BooleanField(default=False)
 receipt_number = models.IntegerField(default=0)

How do I implement this?

like image 425
LaksHmiSekhar Avatar asked Mar 05 '15 10:03

LaksHmiSekhar


1 Answers

ShgGroupLoanMarking.objects.filter(loan__isnull=False)

or

ShgGroupLoanMarking.objects.exclude(loan=None)
like image 177
knbk Avatar answered Oct 10 '22 02:10

knbk