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.
I think the easiest way is to override the save
method. I don't see any reason why it should save to the database twice.
class SoftwareHelp(Help):
about_software = models.ForeignKey('Software')
def save(self, *args, **kwargs):
self.about_software = 'Help for %s' % self.title
return super(SoftwareHelp, self).save(*args, **kwargs)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.