获取至少有一个imag的产品

2024-09-25 16:30:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我的模型是这样的:

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]

Tags: name模型truemodelmodelsproductlengthmax