我有以下型号:
class Keyword(models.Model):
keyword = models.CharField(max_length=100)
class Product(TimeStampedModel):
name = models.CharField(max_length=500, unique=True)
keywords = models.ManyToManyField(Keyword, related_name='products')
我想像这样在自定义字段上INNER JOIN
Product
上表:
SELECT p.name, k.keyword
FROM hotshot_keyword as k
INNER JOIN hotshot_product as p on UPPER(p.name::text) LIKE '%%' || UPPER(k.keyword) || '%%'
WHERE p.id = 1
但是我想在Django orm上做到这一点而不使用原始sql。 这可能吗?