You did not give us your definition of a resource, but if you use tastypie.resources.ModelResource as a base class, this should work:
def obj_create(self, bundle, request=None, **kwargs): bundle = super(GoalResource, self).obj_create( bundle, request, user=request.user) user = request.user user.goals.add( bundle.obj ) user.save() return bundle
This is because the obj_create method of the obj_create class returns a package containing the saved object ( bundle.obj ), and you can manipulate this object in your obj_create method, as shown, and only then return it.
I also suggested that request.user contains a valid User object (i.e. authenticated), you need to make sure that it works for the above, or you should add error handling code for the case when this is not the case.
Hope this helps :)
source share