如何在 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'
我想按如下方式执行mysql查询,
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)
但失败了……知道我怎么了