Closed adamdoty closed 1 year ago
Neat and so simple! Thanks for the tip!
Actually opened Django book last night and it's on page 241 as well (as well as the explanation I was looking for why we'd do an Index on created column - answer: because you would use it a lot with the ORM's order_by()
method)
I took a look (page 268 for the digital edition). Does the truthy statement just prevent the code from running again if while editing the title hasn't changed?
From memory: it first checks if slug is already populated, if not it uses slugify
. Yeah it does that to not do the work again if the value has already been calculated and cached in the DB. He could also have done explicitly if slug is None
Interestingly the stack overflow example does not have that if statement, which brings up a good point: what if the title changes? Then you probably want to rerun the slugify to calculate a new value. So shall we drop the if not self.slug
then?
Cool, you can override the save() method on the model, example: https://stackoverflow.com/a/53664884