Showing posts with label django models python. Show all posts
Showing posts with label django models python. Show all posts

Saturday, March 7, 2009

Django and fetching of foreign keys on attribute access.

Today I noticed something funny about how, and when, Django automatically fetches related objects.

With this simple model;


from django.db import models

class Blog(models.Model):
title = models.CharField(max_length=128)

def __unicode__(self):
return self.title

class Article(models.Model):
body = models.TextField()
blog = models.ForeignKey(Blog)


If you fetch the article and access it's blog.id field, the related object is automatically
fetched, as we can see by investigating the instances _blog_cache field:


>>> blog = Blog.objects.create(title="My Blog")
>>> article = Article.objects.create(body="The quick brown fox...", blog=blog)
>>> article.blog.id
1
>>> article._blog_cache
<Blog: My Blog>


However, if you have no interest in fetching
the related object, there seems to be a workaround: the blog_id field, just check it out;


>>> article = Article.objects.all()[0]
>>> article.blog_id
1
>>> article._blog_cache
AttributeError: 'Article' object has no attribute '_blog_cache'