django-dirtyfields icon indicating copy to clipboard operation
django-dirtyfields copied to clipboard

Option to automatically include DateTimeFields with auto_now enabled

Open damjankuznar opened this issue 4 years ago • 5 comments

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.

damjankuznar avatar Jul 14 '20 09:07 damjankuznar

Coverage Status

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.

coveralls avatar Jul 14 '20 09:07 coveralls

@romgar could you please check this feature

damjankuznar avatar Jul 15 '20 06:07 damjankuznar

Thanks for your contribution @damjankuznar ! I will have a look a bit later.

romgar avatar Jul 16 '20 08:07 romgar

hey @romgar, just bumping this up!

alexanderatallah avatar Oct 17 '20 19:10 alexanderatallah

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.

LincolnPuzey avatar Apr 01 '21 13:04 LincolnPuzey