Python Django使用Django ORM连接多个模型
我在谷歌上搜索并阅读了许多文章,但在多表联接中感到困惑。 我的模特看起来像-Python Django使用Django ORM连接多个模型,python,django,django-models,Python,Django,Django Models,我在谷歌上搜索并阅读了许多文章,但在多表联接中感到困惑。 我的模特看起来像- class ProductCategory(models.Model): category_name = models.CharField(max_length=200,blank=True, null=True, unique=True) category_image = models.ImageField(upload_to='category', null=True, blank=True)
class ProductCategory(models.Model):
category_name = models.CharField(max_length=200,blank=True, null=True, unique=True)
category_image = models.ImageField(upload_to='category', null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)
status = models.CharField(max_length=10, default='Active', choices=status)
def __unicode__(self):
return '%s' % ( self.category_name)
class ProductSubCategory(models.Model):
category = models.ForeignKey(ProductCategory)
sub_category_name = models.CharField(max_length=200,blank=True, null=True, unique=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)
sub_category_image = models.ImageField(upload_to='subcategory', null=True, blank=True)
status = models.CharField(max_length=10, default='Active', choices=status)
def __unicode__(self):
return '%s' % ( self.sub_category_name)
class Product(models.Model):
category = models.ForeignKey(ProductCategory)
sub_category = models.ForeignKey(ProductSubCategory)
product_name = models.CharField(max_length=200,blank=True, null=True)
product_price = models.FloatField(default=0)
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)
# is_discountable = models.CharField(max_length=3, default='Yes', choices=option)
status = models.CharField(max_length=10, default='Active', choices=status)
def __unicode__(self):
return '%s' % ( self.product_name)
class ProductColor(models.Model):
product = models.ForeignKey(Product)
product_color = models.ForeignKey(Color, related_name='product_color_id', blank=True, null=True)
product_size = models.ForeignKey(Size, related_name='product_size_id', blank=True, null=True)
class ProductImages(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True)
product_image = models.ImageField(upload_to='images', null=True, blank=True)
现在在视图中,我想根据类别和子类别获取产品过滤器,其中包含所有图像和颜色。查询类似于-
SELECT product.*, productcolor.*, productimage.* FROM product
LEFT JOIN productcolor ON productcolor.product_id = product.id
LEFT JOIN productcolor.product_id = product.id
LEFT JOIN productimage ON productimage.product_id = product.id
WHERE product.category_id=1 and product.sub_category_id=1
根据您的SQL,这就可以了
Product.objects.filter(category=<category>, sub_category=<sub_category>) \
.prefetch_related('productcolor_set', 'productimages_set')
Product.objects.filter(类别=,子类别=)\
.prefetch_related('productcolor_set'、'productimages_set')
此查询将预取所有(带有)ProductColor
和ProductImages
与Product
相关的ProductImages
,它们将分别存储在ProductColor\u集
和ProductImages\u集
中
另外,我建议将您的
ProductImages
模型重命名为ProductImages
,因为它只代表一个产品图像。您的需求似乎很广泛。你能详细说明一下吗?提供示例。@vishes\u shell刚刚更新了问题。@vishes\u shell我已经尝试了,但仍然没有得到方便的解决方案:(你是什么意思?我尝试了很多,但输出不符合预期。)在与选择相关的“productimages\u set”、“productcolor\u set”中给出。选项包括:类别、子类别_category@V.Khakhil我的错误,早上的错误,更新了答案。预回迁相关和选择相关仅从具有where条件的产品中选择。选择cart\u产品id
,cart\u产品
类别\u id
,cart\u product
,cart\u product
,ISBN
,cart\u product
,product\u name
,cart\u product
,cart\u product
,产品描述
,产品评级
,cart\ode>创建于
,购物车产品
更新于
,购物车产品
状态
来自购物车产品
其中(购物车产品
类别id
=1和购物车产品
子类别id
=1)@不止这些,你们应该阅读我已经附在答案后面的文件。
Product.objects.filter(category=<category>, sub_category=<sub_category>) \
.prefetch_related('productcolor_set', 'productimages_set')