Django TastyPie, how to trigger an action after a POST with ManyToMany fields?

I have a REST API built using Django and TastyPie. My goal is to add a task to the work queue when new data is sent to a specific model.

I was about to connect to post_save and then run, but the model contains ManyToMany relationships, and so post_save starts before the m2m relationships are updated and the connection to the m2m_changed signal seems messy. I receive several signaling events, and my code will have to check the instance after each of them and try to determine if it is ready to fire the event. Some of the ManyToMany fields may be null, so when I get the m2m_changed signal, I really don’t know for sure if this would save.

Is there a proper way to do this? Does TastyPie allow you to hook into a POST event and do something at the end? All I found points me to post_save events to do this.

Does Django have a way to tell me when all m2m updates for an instance of this model are complete?

+4
source share
2 answers

If you use POST , then obj_update() does not work for me. That work used obj_create() as follows:

 class Resource(ModelResource): def obj_create(self,bundle,**kwargs): bundle = super(Resource,self).obj_create(bundle,**kwargs) # Add code here return bundle 

It should be noted that request not included. I tried this and it gave me an error.

+7
source

You can override the obj_update method

 class Resource(ModelResource): def obj_update(self, bundle, request, **kwargs): bundle = super(Resource, self).obj_update(bundle, **kwargs) # queue your task here return bundle 
+6
source

All Articles