如何在django中从两个表中选择结果

如何在django中从两个表中选择结果,django,django-models,Django,Django Models,如何在Django中从两个表中选择结果 我的模型如下: from django.db import models from apps.admin.product.models import Product class Cabinet(models.Model): id = models.IntegerField(primary_key=True) cabinet_name = models.CharField(max_length=45L, blank=True) cl

如何在Django中从两个表中选择结果

我的模型如下:

from django.db import models
from apps.admin.product.models import Product

class Cabinet(models.Model):
    id = models.IntegerField(primary_key=True)
    cabinet_name = models.CharField(max_length=45L, blank=True)
    class Meta:
        db_table = 'cabinet'




class ProductCabinetConstruction(models.Model):
    id = models.IntegerField(primary_key=True)
    product = models.ForeignKey(Product, null=True, blank=True)
    cabinet_construction = models.ForeignKey(Cabinet, null=True, blank=True)
    size = models.FloatField(null=True, blank=True)
    class Meta:
        db_table = 'product_cabinet_construction'
SELECT DISTINCT (cabinet.cabinet_name), product_cabinet_construction.product_id FROM product_cabinet_construction, cabinet WHERE product_cabinet_construction.product_id = 33
我想执行如下mysql查询:

from django.db import models
from apps.admin.product.models import Product

class Cabinet(models.Model):
    id = models.IntegerField(primary_key=True)
    cabinet_name = models.CharField(max_length=45L, blank=True)
    class Meta:
        db_table = 'cabinet'




class ProductCabinetConstruction(models.Model):
    id = models.IntegerField(primary_key=True)
    product = models.ForeignKey(Product, null=True, blank=True)
    cabinet_construction = models.ForeignKey(Cabinet, null=True, blank=True)
    size = models.FloatField(null=True, blank=True)
    class Meta:
        db_table = 'product_cabinet_construction'
SELECT DISTINCT (cabinet.cabinet_name), product_cabinet_construction.product_id FROM product_cabinet_construction, cabinet WHERE product_cabinet_construction.product_id = 33
试着

models.ProductCabinetConstruction.objects.select_related().filter(product=productObj.id)

但是失败了……你知道我怎么了吗?

我不知道你的代码为什么不工作。。。。代码中没有错误,代码将返回包含ProductCabinetConstruction列表的queryset。您必须循环此queryset以获取每个元素

pcc = ProductCabinetConstruction.objects.select_related().filter(product=33)
我认为您没有使用循环来获取每个元素

pcc = ProductCabinetConstruction.objects.select_related().filter(product=33)
像这样循环获取每个元素

for productcc in pcc:
    pro = productcc.product #is the product
    cab = productcc.cabinet_construction  # is the cabinet
试试这些:

ProductCabinetConstruction.objects.select_related('product', 'cabinet_construction').filter(product__product_id=33).distinct('cabinet_construction__cabinet_name')


什么失败了
pcc=ProductCabinetConstruction.objects。选择与之相关的(“产品”)。过滤器(product\uu id=productObj.id)
应该可以工作。如果您想访问该对象,则可以访问
pcc.product
select\u related
不会影响结果,它只是有助于提高查询效率,这
建模.ProductCabinetConstruction.objects.values('cabinet\u construction\u cabinet\u name')。筛选(product\u id=productObj.id).distinct()
但我的mysql查询-“select distinct(cabinet.cabinet\u name)”,product_cabinet_construction.product_id来自product_cabinet_construction,其中product_cabinet_construction.product_id=33”的机柜将返回所有机柜名称(如果机柜构造中不存在)。如果我们将其切换到django,它只返回CAB和CAB_构造中的公共值:(