Skip to content

Commit

Permalink
Refactored obj_update with identifiers
Browse files Browse the repository at this point in the history
There is no need to call `full_hydrate` when we only want to convert
identifiers into Python objects.

This avoids side effects on `obj_update` and adds the possibility to
flag `full_hydrate` as called, for avoiding performance hits.

This needs tests proposed in django-tastypie#658 and helps closing django-tastypie#390
  • Loading branch information
maraujop committed Sep 7, 2012
1 parent 94ceb21 commit 83dc1fa
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions tastypie/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1912,21 +1912,32 @@ def obj_update(self, bundle, request=None, skip_errors=False, **kwargs):
A ORM-specific implementation of ``obj_update``.
"""
if not bundle.obj or not self.get_bundle_detail_data(bundle):
# 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.
# Kwargs here represent uri identifiers Ex: /repos/<user_id>/<repo_name>/
# We need to turn those identifiers into Python objects for looking them up in the DB
try:
bundle.obj = self.get_object_list(bundle.request).model()
# Override data values, we rely on uri identifiers
bundle.data.update(kwargs)
bundle = self.full_hydrate(bundle)
lookup_kwargs = kwargs.copy()

for key in kwargs.keys():
if key == self._meta.detail_uri_name:
lookup_kwargs = {}
for identifier in kwargs:
if identifier == self._meta.detail_uri_name:
lookup_kwargs[identifier] = kwargs[identifier]
continue
elif getattr(bundle.obj, key, NOT_AVAILABLE) is not NOT_AVAILABLE:
lookup_kwargs[key] = getattr(bundle.obj, key)
else:
del lookup_kwargs[key]

field_object = self.fields[identifier]

if field_object.readonly is True:
continue

# Check for an optional method to do further hydration.
method = getattr(self, "hydrate_%s" % identifier, None)
if method:
bundle = method(bundle)
if field_object.attribute:
value = field_object.hydrate(bundle)

lookup_kwargs[identifier] = value
except:
# if there is trouble hydrating the data, fall back to just
# using kwargs by itself (usually it only contains a "pk" key
Expand Down

0 comments on commit 83dc1fa

Please sign in to comment.