question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Undocument breaking changes in FieldTracker in Version 4.1

See original GitHub issue

Problem

The Release 4.1, after moving the patch from the save() to the save_base() method in #404 (@tumb1er FYI) introduced a serioulsy breaking change that was neither announced nor documented (and was obviously not tested brefore). It would have deserved a 5.x update IMHO.

Since the values of the tracker are restored after the call to the save_base() method, any check/assumption made on the tracker inside the save() Method but after the super().save() call fails, as the tracker is then “new/empty”. This is especially problematic when tracking changes depending on related objects, when working with nested serializers per example.

Environment

  • Django Model Utils version: <4.1 VS 4.1+

Code examples

class MyModel(Model):
        name = models.CharField(max_length=64)
        tracker = FieldTracker()

        def save(self, *args, **kwargs):
            if self.tracker.changed('name'):
                 print("True")
            super().save(*args, **kwargs)
            if self.tracker.changed('name'):
                 print("True")

Just calling MyModel.create() would result in 2 prints in version <4.1 and in only one in version 4.1+ (the second check will fail as the tracker is resetted on the save_base() call inside the `super().save() call)

Discussion / proposal

@auvipy A work-around is to test before the call to save() and then use the variable afterwards for such cases.

But I honestly find it quite a dramatic regression for the IMHO limited benefit of being able to modify update_fields in the save() Method.

I would seriously prefer to restore the previous behaviour of patching the save() Method, but still add a functionality to support overriding update_fields: I would prefer to add a new kwarg _always_update to FieldTracker where you could define the list of fields that you always want to update. I think also in term of design and readbility it would be a better feature. @tumb1er what do you think about the suggestion? If you validate it, I could draft a PR for this

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
tumb1ercommented, Aug 27, 2021

I’ll try to find some time at weekend.

1reaction
MRigalcommented, Aug 27, 2021

@tumb1er we discussed offline with @gregorgaertner and we do agree that this is a great idea!

If you’re willing to, you could start with a PR and we could both review it. Else I was thinking to try to work on it, but I’m not sure when I’ll get the time for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Medi-Cal Expansion Provided 286,000 Undocumented ...
This latest expansion of Medi-Cal coverage brings California closer to Governor Newsom's promise of universal access to health care coverage, ...
Read more >
U.S. Supreme Court wrestles over Biden's immigration ...
The Biden administration instructed immigration agents to focus on deporting undocumented immigrants who have been convicted of felonies or ...
Read more >
The effects of parental undocumented status on families and ...
We end with recommendations for changes to ameliorate the impact of enforcement on the lives of immigrant families and their children.
Read more >
5 MYTHS ABOUT UNDOCUMENTED IMMIGRANTS - CT.gov
MYTH #1: Anyone who enters the country illegally is a criminal. Facts: Being an undocumented (or “illegal”) immigrant has been, until now, only...
Read more >
U.S. Citizen Children Impacted by Immigration Enforcement
This fact sheet provides an overview of the children who are impacted by immigration enforcement actions, the challenges and risk factors that these...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found