1

Problem with a post_save signal?

class Book(models.Model):
    room = models.ForeignKey(Room, on_delete=models.CASCADE, null=False)
    library = models.ForeignKey(Library, on_delete=models.CASCADE, null=False)
    created_at = models.DateTimeField(auto_now_add=True)
    tracker = FieldTracker()


def update_service(sender, instance, **kwargs):

    main_library = Library.object.get(id=1)

if not instance.library == library:
    for book in instance.book_set.all():
       book.delete()

post_save.connect(update_service, sender=Library)

lib/python3.7/site-packages/model_utils/tracker.py in set_saved_fields(self, fields) 106 self.saved_data = self.current() 107 else: --> 108 self.saved_data.update(**self.current(fields=fields)) 109 110 # preventing mutable fields side effects AttributeError: 'FieldInstanceTracker' object has no attribute 'saved_data'

Jorge
  • 105
  • 9

2 Answers2

2

Set fields attribute of trackerfield.

Issue has been fixed in last version of package

class Book(models.Model):
    cls = models.ForeignKey(Class, on_delete=models.CASCADE, null=False)
    library = models.ForeignKey(Library, on_delete=models.CASCADE, null=False)
    created_at = models.DateTimeField(auto_now_add=True)
    tracker = FieldTracker(fields=['XXXX'])

Fields have to contains list of fields you want to track.

class is a reserved keyword. Be care !

Lucas Grugru
  • 1,664
  • 1
  • 4
  • 18
  • True.. it was a mistake to make the case.. i will rename it for something else... fields means that i want to track a particular field or it has to be one particular field of the model ? – Jorge Sep 26 '22 at 14:25
  • 1
    FieldTracker is a field for tracking model fields change. fields is the list of field name you want to track. Everything is explained in the documentation here: https://django-model-utils.readthedocs.io/en/latest/utilities.html#field-tracker – Lucas Grugru Sep 26 '22 at 14:39
1

I found the solution, I needed it to update some packages.

the first one was:

django-model-utils==4.1.1 -> django-model-utils==4.2.0

Then It also brought me some dependency with 2 other packages.

openwisp-utils==0.4.5 -> openwisp-utils==1.0.3

swapper==1.1.2 -> swapper==1.3.0

After I upgraded the libraries, the problem disappeared.

Also I did not need to add the fields to Tracker:

tracker = FieldTracker(fields=['XXXX'])

I let it the by default:

tracker = FieldTracker()

Just be careful with the libraries, because I spent a lot of times thinking that it was my code but in the end it was a library issue.

Jorge
  • 105
  • 9