adamdoty / homebaker

A baked-goods, birthday-treat, tracking app I made for my wife.
3 stars 0 forks source link

add auto-fill slug based on treat name #22

Closed adamdoty closed 1 year ago

bbelderbos commented 1 year ago

Cool, you can override the save() method on the model, example: https://stackoverflow.com/a/53664884

adamdoty commented 1 year ago

Neat and so simple! Thanks for the tip!

bbelderbos commented 1 year ago

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)

adamdoty commented 1 year ago

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?

bbelderbos commented 1 year ago

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

bbelderbos commented 1 year ago

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?