skip to Main Content

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’

2

Answers


  1. Chosen as BEST ANSWER

    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.


  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 !

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search