Zahidul Islam
Zahidul Islam

Reputation: 2326

How to get data where ImageField in not null in django?

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.

Upvotes: 0

Views: 382

Answers (2)

Bartosz Stasiak
Bartosz Stasiak

Reputation: 1632

Try this:

Services.objects.exclude(bg_image='')

Upvotes: 1

korku02
korku02

Reputation: 7

Try with this exclude instead

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

Upvotes: 0

Related Questions