Created
June 24, 2012 22:55
-
-
Save jimrollenhagen/2985369 to your computer and use it in GitHub Desktop.
property example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Thing(models.model): | |
something = models.CharField(null=True) | |
# ... | |
@property | |
def something_display(self): | |
if self.something is None: | |
return 'something else' | |
return self.something | |
##### | |
>>> thing = Thing() | |
>>> thing.something | |
None | |
>>> thing.something_display | |
'something else' | |
>>> thing.something = 'yay' | |
>>> thing.something | |
'yay' | |
>>> thing.something_display | |
'yay' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment