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

Make FIELDS_TO_CHECK uses casual field name in case of ForeignKey #136

Merged
merged 1 commit into from Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/dirtyfields/dirtyfields.py
Expand Up @@ -42,7 +42,7 @@ def _as_dict(self, check_relationship, include_primary_key=True):
deferred_fields = self.get_deferred_fields()

for field in self._meta.fields:
if self.FIELDS_TO_CHECK and (field.get_attname() not in self.FIELDS_TO_CHECK):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mounirmesselmeni as explained in the discussion on this PR, can we make it backward compatible by allowing both with and without _id?
Thanks!

if self.FIELDS_TO_CHECK and (field.name not in self.FIELDS_TO_CHECK):
continue

if field.primary_key and not include_primary_key:
Expand Down Expand Up @@ -138,8 +138,7 @@ def reset_state(sender, instance, **kwargs):
if update_fields:
for field_name in update_fields:
field = sender._meta.get_field(field_name)
if not FIELDS_TO_CHECK or \
(field.get_attname() in FIELDS_TO_CHECK):
if not FIELDS_TO_CHECK or (field.name in FIELDS_TO_CHECK):

if field.get_attname() in instance.get_deferred_fields():
continue
Expand Down
2 changes: 1 addition & 1 deletion tests/models.py
Expand Up @@ -129,7 +129,7 @@ class TestModelWithSpecifiedFieldsAndForeignKey(DirtyFieldsMixin, models.Model):
boolean1 = models.BooleanField(default=True)
boolean2 = models.BooleanField(default=True)
fk_field = models.OneToOneField(TestModel, null=True)
FIELDS_TO_CHECK = ['fk_field_id']
FIELDS_TO_CHECK = ['fk_field']


class TestModelWithM2MAndSpecifiedFields(DirtyFieldsMixin, models.Model):
Expand Down