You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems like FieldTracker isn't able to detect changes on a field anymore. I'm not sure if it's because there were changes made to its implementation that prevents it from observing a field's change state when the field's model instance is being saved.
What I would do is override the model's save method and add a "post-save hook" where I check if the field has changed, and if so, then I'll run some additional logic.
Let's take for example, I have an Employee model that's tied to an extended auth user model. If an employee is activated/deactivated, I would like the employee's associated user profile to also be activated/deactivated.
This post-save logic would previously work on django-model-utils==4.0.0, as my unit test would pass:
classEmployeeModelTest(TestCase):
deftest_employee_active_flag_updates_login_account_active(self):
# prep dataemployee=EmployeeFactory.create()
user=ProfileFactory.create(employee_profile=employee)
# first ensure user is activeself.assertTrue(user.is_active)
employee.is_active=Falseemployee.save()
# user should also now be deactivateduser.refresh_from_db()
self.assertFalse(user.is_active) # fails here
For completeness (to rule out factoryboy), I also did a manual test, where I would log into django admin to deactivate the employee. The employee's associated user account still wouldn't deactivate.
Environment
Django Model Utils version: 4.1.1
Django version: 2.2.22
Python version: 3.8.3
Other libraries used, if any: factory-boy==2.12.0 for testing
The text was updated successfully, but these errors were encountered:
Ok. Thanks for clarifying the order of where the reset state is executed. I want to avoid using signals as much as possible so I'll just move the check before the super().save call.
Problem
It seems like FieldTracker isn't able to detect changes on a field anymore. I'm not sure if it's because there were changes made to its implementation that prevents it from observing a field's change state when the field's model instance is being saved.
What I would do is override the model's
save
method and add a "post-save hook" where I check if the field has changed, and if so, then I'll run some additional logic.Let's take for example, I have an
Employee
model that's tied to an extended auth user model. If an employee is activated/deactivated, I would like the employee's associated user profile to also be activated/deactivated.This post-save logic would previously work on
django-model-utils==4.0.0
, as my unit test would pass:For completeness (to rule out factoryboy), I also did a manual test, where I would log into django admin to deactivate the employee. The employee's associated user account still wouldn't deactivate.
Environment
factory-boy==2.12.0
for testingThe text was updated successfully, but these errors were encountered: