Home > OS >  How to get data where ImageField in not null in django?
How to get data where ImageField in not null in django?

Time:02-17

context_processor.py file

    def context_processor(request):
    context={}
    context['services'] = Services.objects.filter(bg_image__isnull=False)
    return context

models.py

class Services(models.Model):
    title = models.CharField(max_length=250)
    bg_image = models.ImageField(upload_to='services/', null=True, blank=True)
    active = models.BooleanField(default=True)
    def __str__(self):
        return self.title

I have 7 record where 5 of them has bg_image rest of 2 bg_image are null. when I query the above code show 7 record has bg_image. who can I query property to get only those records where bg_image is not null? For queryset when I use within bracket active=True it works fine.

CodePudding user response:

Try this:

Services.objects.exclude(bg_image='')

CodePudding user response:

Try with this exclude instead

def context_processor(request):
    context={}
    context['services'] = Services.objects.filter().exclude(big_image=None)
    return context
  • Related