Given a model
class Template(models.Model):
colour = models.CharField(default="red", blank = True, null=True)
How can I arrange it so that any access to colour either returns the value stored in the field, or if the field is blank/null then it returns red?
The
default=redwill put "red" in the field when it's first created, but if it's then edited to blank I'd like access to it to return "red", not ""
Updated: I tried the properties solution suggested below, but I'm using JSON serialisation to implement a REST API and the properties (e.g. colour
) don't get serialised, and serialising the _colour
breaks the API