Skip to content

Instantly share code, notes, and snippets.

@dbtsai
Created June 22, 2012 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbtsai/2970244 to your computer and use it in GitHub Desktop.
Save dbtsai/2970244 to your computer and use it in GitHub Desktop.
tastypie obj_update
def obj_update(self, bundle, request=None, skip_errors=False, **kwargs):
"""
A ORM-specific implementation of ``obj_update``.
"""
# import pdb
# pdb.set_trace()
setattr(bundle.obj,self._meta.detail_uri_name,None)
if not bundle.obj or not getattr(bundle.obj,self._meta.detail_uri_name):
# Attempt to hydrate data from kwargs before doing a lookup for the object.
# This step is needed so certain values (like datetime) will pass model validation.
try:
bundle.obj = self.get_object_list(bundle.request).model()
bundle.data.update(kwargs)
bundle = self.full_hydrate(bundle)
lookup_kwargs = kwargs.copy()
setattr(bundle.obj,self._meta.detail_uri_name, bundle.data[self._meta.detail_uri_name])
for key in kwargs.keys():
if key == 'pk':
continue
elif getattr(bundle.obj, key, NOT_AVAILABLE) is not NOT_AVAILABLE:
lookup_kwargs[key] = getattr(bundle.obj, key)
else:
del lookup_kwargs[key]
except:
# if there is trouble hydrating the data, fall back to just
# using kwargs by itself (usually it only contains a "pk" key
# and this will work fine.
lookup_kwargs = kwargs
try:
bundle.obj = self.obj_get(bundle.request, **lookup_kwargs)
except ObjectDoesNotExist:
raise NotFound("A model instance matching the provided arguments could not be found.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment