user3560295
user3560295

Reputation: 1

Django - filtering in views

I am working on an basic application and I`m stuck at a displaying some info. Please take a look:

Models:

class Companies(models.Model):

    name = models.CharField()
    address = models.CharField()

    def __unicode__(self):
         return self.name


class Payments(models.Model):

    company = models.ForeignKey(Companies)    
    year = models.CharField(choices=YEAR)    
    month = models.CharField(choices=MONTHS)    
    date = models.DateField(auto_now_add=True)

I want a view in which to display ONLY the companies that did not pay the monthly fee.

So I`ve started like this:

def checks(request):    
    i = datetime.datetime.now()    
    an_c = i.strftime('%Y')    
    comp = Companies.objects.all()    
    pay1 = Payments.objects.filter(an=an_c, month='01')

But in the template I do not know how to filter the "comp" list. I want to display in the template all the records from "comp" except that ones with the id/pk which can be find in the "pay1.company"

Upvotes: 0

Views: 64

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

You wouldn't do that in the template. Do the whole thing in the view:

pay1 = Payments.objects.filter(an=an_c, month='01')
comp = Companies.objects.exclude(payments__in=pay1)

(Style note: Django model classes are usually named in the singular, not the plural.)

Upvotes: 2

Related Questions