Select_related does not join columns marked with nulll = True

I have a Django model -

class NoticedUser(models.Model):
    user = models.ForeignKey(User, null=False)
    text = models.CharField(max_length=255, null=True)
    photo = models.ForeignKey(Photo, null=True, blank=True)
    article = models.ForeignKey(Article, null=True, blank=True)
    date = models.DateTimeField(default=datetime.now)

      

When I try to fetch objects i.e. with NoticedUser.objects.all (). select_related (), the resulting query does not contain joins with the "photo" and "article" tables. I've looked at the Django sources and it seems that fields containing null = True should result in a left join instead of an inner join, but I didn't find why the corresponding left joins don't appear in the query result. This causes additional queries when displaying related objects, and it is also not possible to perform custom joins for the "photo" and "article" tables used in our project.

Actually, joins only show up for fields with null = False, but I cannot change the field definitions.

How can I add joins for fields with null = True to the resulting query? The Django version I am using is 1.0.2.

Thank.

+2


source to share


1 answer


It does not follow these relationships by default when used select_related()

without parameters. You must explicitly list the names:



NoticedUser.objects.all().select_related('article', 'photo')

      

+4


source







All Articles