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')