There has been some discussion about removing
auto_now_add some time ago.
auto_now_add can be replaced be using a callable default value,
auto_now can't. So I wrote this litte function for my current project (older ones still use
auto_add) to fill the gap...but I'm not sure if
auto_now will be removed at all.
1 2 3 4 5 6 7 8
# Takes model and fieldname and sets field to current datetime on pre_save def auto_now_on_save(model, fieldname): from django.db.models import signals from django.dispatch import dispatcher def _update_datefield(instance): from datetime import datetime setattr(instance, fieldname, datetime.now()) dispatcher.connect(_update_datefield, signal=signals.pre_save, sender=model, weak=False)
More like this
- Treat empty value on DateRangeFilter as a filter for non filled dates by kmmbvnr 1 month, 1 week ago
- Django Settings Assignment Expressions aka Walrus Operator example by webology 2 months, 1 week ago
- codigo alto nivel by MrRocklion 4 months ago
- Load template from specific app by Krzysiek555 4 months, 3 weeks ago
- PostgreSQL JSON subqueries by dolamroth 4 months, 3 weeks ago