select_related on more then 2 two tables


I have the following model

blogs = models.ForeignKey(‘blogs’, models.DO_NOTHING)
channel = models.ForeignKey(‘channel’, models.DO_NOTHING)

class Blogs(models.Model):
id = models.AutoField(primary_key=True)
date_posted = models.DateTimeField()
title = models.CharField(max_length=255)
attachments = models.ForeignKey(‘attachments’, models.DO_NOTHING)

class Attachments(models.Model):
id = models.AutoField(primary_key=True)
file = models.TextField()
type = models.CharField(max_length=10)

if I write
A = Channels.objects.select_related().filter(channel = 1).order_by(’
I get data related to table blogs but not related to attachments but when I see query I can Attachments in the query

I get data from the three tables. but I’ not able to add filters from third table (Attachments)
example :
A = Channels.objects.select_related().filter(channel = 1).filter(attachments__type = “jpg”).order_by(’

Any help on this or direction to right document is very much appreciated

Firstly, make sure you pass explicitly that you want the nested link to select_related:

select_related("blogs", "blogs__attachments")

Then, the real error is that you want to filter in blog__attachments__type - you have to include the first foreign key in the relationship there, as Attachments is not directly on Channel.