私は2つのテーブルを持っています:
- 商品(ID、名前)
- 属性 (id、product_id、名前、値)
商品を検索する際、テーブル「属性」を2回結合するにはどうすればよいですか? 後でページングされるため、それらは 1 つのクエリに含まれている必要があります。
例: 2 つの属性が必要な商品を検索します。1 つは 用name=att1, value=value1
、もう 1 つは 用name=att2, value=value2
です。
ソースコード:
class Product(models.Model):
product_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100, null=False)
class Attribute(models.Model):
attribute_id = models.AutoField(primary_key=True)
product = models.ForeignKey(Product, null=False)
name = models.CharField(max_length=100, null=False)
value = models.CharField(max_length=100, null=False)
機能しないクエリ:
Product.objects.select_related().filter('attribute__name': 'n1', 'attribute__value':'v1').filter('attribute__name': 'n2', 'attribute__value':'v2')