Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取至少具有一个图像的产品_Python_Django - Fatal编程技术网

Python 获取至少具有一个图像的产品

Python 获取至少具有一个图像的产品,python,django,Python,Django,我的模型如下所示: class Product(models.Model): name = models.CharField(max_length=100) ... class ProductImage(models.Model): thumbnail = models.ImageField(...) ... product = models.ForeignKey(Product, related_name='images', blank=True, n

我的模型如下所示:

class Product(models.Model):
    name = models.CharField(max_length=100)
    ...

class ProductImage(models.Model):
    thumbnail = models.ImageField(...)
    ...
    product = models.ForeignKey(Product, related_name='images', blank=True, null=True)
我需要更改以下内容,使其仅获取至少具有一个图像的产品:

products = Product.objects.filter(status='PU').order_by('?')[:4]

注释,然后对注释进行过滤。


注释,然后对注释进行过滤。


试试这个:

products = Product.objects.filter(status='PU', images__isnull=False).order_by('?')[:4]

试试这个:

products = Product.objects.filter(status='PU', images__isnull=False).order_by('?')[:4]

不完全,需要更改
'productimage'
以匹配其
相关的\u名称
,因此
图像计数=计数('images')
不完全,需要更改
'productimage'
以匹配其
相关的\u名称
,因此
图像计数=计数('images')