django-dirtyfields
django-dirtyfields copied to clipboard
Option to automatically include DateTimeFields with auto_now enabled
Add an option to automatically include DateTimeField
and DateField
with auto_now
set to True
to a list of dirty fields in case any other field has changed.
Coverage increased (+1.002%) to 97.554% when pulling 072c17201298cf44e6464120d231c026d3cdb83a on damjankuznar:feature/option_to_include_datetime_fields_with_auto_now into 52a7586652efe094ce4888c1155ce7405e0f788e on romgar:develop.
@romgar could you please check this feature
Thanks for your contribution @damjankuznar ! I will have a look a bit later.
hey @romgar, just bumping this up!
Hi @damjankuznar,
The django docs say for fields with auto_now=True
The field is only automatically updated when calling Model.save()
For the first part of your change - adding the option to get_dirty_fields()
, since django-dirtyfields aims to track fields that are different in memory vs the database, I am reluctant to add the option. This is because the field in memory always matches the database, until Model.save()
is called, when it is updated in memory and then the database it immediately updated - so the field is effectively never dirty.
For the second part of your change - adding the option to save_dirty_fields()
, rather than add a special case for auto_now
fields, I think adding a generic extra_update_fields
argument, eg
def save_dirty_fields(self, extra_update_fields=None):
update_fields = set(self.get_dirty_fields(check_relationship=True).keys())
if extra_update_fields:
update_fields.update(extra_update_fields)
self.save(update_fields=update_fields)
Is a better, more flexible option. This allows including other fields that might get modified by Model.save()
You could override save_dirty_fields()
in your models if you want certain auto_now
fields to be always included.