Sunday, 24 March 2019

How do I write a Django query that uses a complex "on" clause in its inner join?

I'm using Django, Python 3.7, and PostGres 9.5. I have these models ...

class Article(models.Model):
    ...
    label = models.TextField(default='', null=True)


class Label(models.Model):
    name = models.CharField(max_length=200)

I want to write a Django query that retrieves all the articles whose label contains a name from the Labels table. In PostGres, I can structure my query like so ...

select a.* from myapp_article a join myapp_label l on a.label ilike '%' || l.name || '%';

but I have no idea how to pull this off in Django on account of the "on" clause and "ilike". How do I pull this off?



from How do I write a Django query that uses a complex "on" clause in its inner join?

No comments:

Post a Comment