如何在 Django 中获取 ImageField 不为空的数据?

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

context_processor.py 文件

    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

我有 7 条记录,其中 5 条 bg_image 其余 2 条 bg_image 为空。当我查询上面的代码时,显示 7 条记录有 bg_image。谁可以查询 属性 以仅获取 bg_image 不为空的那些记录?对于查询集,当我在括号 active=True 中使用时,它工作正常。

试试用这个排除代替

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

试试这个:

Services.objects.exclude(bg_image='')