tags:

views:

68

answers:

1

Hi All!

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 the objects, i.e. with NoticedUser.objects.all().select_related(), resulting query does not contain joins with 'photo' and 'article' tables. I have looked through the Django sources and it seems that fields containing null=True should result in left join instead of inner join, but I have not found why appropriate left joins do not appear in resulting query. This causes additional queries while displaying related objects, as well as there is no possibility to perform custom joins for 'photo' and 'article' tables used in our project.

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

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

Thanks.

+3  A: 

It doesn't follow these relations by default when using select_related() with no parameters. You have to explicitly specify the names:

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