How do I easily create a calculated field in a Django model?

Viewed 1313

No, I don't want a property. I really don't. What I want is what I asked.


Due to subclassing requirements, I'm looking for a way to generate one field from a set of two others and store this computation in the database. Not a property in Python, not an SQL calculation, a pre-calculated field that is updated on save and stored, as is, in the database.

For example:

class Help(models.Model):
    title = models.TextField()
    body = models.TextField()

class SoftwareHelp(Help):
    about_software = models.ForeignKey('Software')

Regardless of what a user enters in the title field, I want it to say "Help for " once save is clicked. In reality, the code has more fields, but this explains the principle.

I know its possible to do this by overriding the save() method, but wanted to make sure I wasn't saving to the database twice, and want to know if there is another better way.

1 Answers
Related