Get original object in obj_update?

10 views
Skip to first unread message

Stodge

unread,
Mar 16, 2017, 10:33:28 AM3/16/17
to Tastypie
Is it possible to get the original version of the object in obj_update? I need to check if a flag is being toggled to perform further processing. My code:

    def obj_update(self, bundle, **kwargs):
       
       
# Update the state of all event groups for this strategy
       
# if required.
        instance
= self.obj_get(bundle, **kwargs)
       
       
# Get the original version.
        original
= EventStrategy.objects.get(pk=instance.pk)
       
       
print("Original: %s, %s ==> %s" % (original.name, original.active, instance.active))
       
       
# Check active state transition.
       
if original.active != instance.active:
               
           
print("Toggling %s" % instance.name)
           
           
# Update groups.
           
for group in instance.event_groups.all():
               
group.active = not group.active
               
group.updated = datetime.datetime.utcnow()
               
group.save()
       
       
return super(EventStrategyResource, self).obj_update(bundle, **kwargs)


When, I do a PUT and set the flag to FALSE, the output from the first print statement is:

Original: Test, True ==> True

Thanks



Rene Zelaya

unread,
Mar 16, 2017, 11:12:35 AM3/16/17
to Tastypie
Hi,

What happens if you try to access it with
original = bundle.obj
?

Also, you mean that the original object instance has the flag set to True and the parameter in your request is telling it to be changed to False when doing the update?

Best,
Rene
Reply all
Reply to author
Forward
0 new messages