Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to automatically include DateTimeFields with auto_now enabled #152

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

damjankuznar
Copy link

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.

@coveralls
Copy link

coveralls commented Jul 14, 2020

Coverage Status

Coverage increased (+1.002%) to 97.554% when pulling 072c172 on damjankuznar:feature/option_to_include_datetime_fields_with_auto_now into 52a7586 on romgar:develop.

@damjankuznar
Copy link
Author

@romgar could you please check this feature

@romgar
Copy link
Owner

romgar commented Jul 16, 2020

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

@alexanderatallah
Copy link

hey @romgar, just bumping this up!

@LincolnPuzey
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants