Skip to content

Instantly share code, notes, and snippets.

@bohde
Created August 23, 2011 15:23
Show Gist options
  • Select an option

  • Save bohde/1165415 to your computer and use it in GitHub Desktop.

Select an option

Save bohde/1165415 to your computer and use it in GitHub Desktop.
Adding a property to a model
class MyUserModel(User):
@property
def room(self):
# Don't run the query twice
if not hasattr(self, '_room'):
try:
self._room = self.lease_set.select_related('room').order_by('-date')[0].room
except Lease.DoesNotExist:
self._room = None
return self._room
class MyUserModel(User):
@property
def room(self):
# Don't run the query twice
if not hasattr(self, '_room'):
try:
self._room = self.lease_set.select_related('room').order_by('-date')[0].room
except Lease.DoesNotExist:
self._room = None
return self._room
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment