Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelResource.obj_update calls full_hydrate twice #390

Closed
brianhv opened this issue Feb 28, 2012 · 17 comments
Closed

ModelResource.obj_update calls full_hydrate twice #390

brianhv opened this issue Feb 28, 2012 · 17 comments
Milestone

Comments

@brianhv
Copy link

brianhv commented Feb 28, 2012

Starting in this commit obj_update calls full_hydrate twice. This means that if non-idempotent transformations happen in a user's hydrate() functions it will result in an attribute that isn't in the expected state.

@tarequeh
Copy link

This can be fixed with a simple flag tracking whether the full_dehydrate was already called. I have a tentative patch here. Working on tests. Feel free to comment. tarequeh@6f95afb

@ghost
Copy link

ghost commented Jul 11, 2012

I've got the exact same problem. My hydrate process is being called twice. Since I'm converting some fields into an internal representation (and vice versa on the way out), it bombs out on the second call.

@bohde
Copy link
Contributor

bohde commented Aug 14, 2012

Looking into this, it's a bit more complicated that tracking if full_hydrate has already been called. full_hydrate is used to prepare data to lookup objects in the database based upon the provided data, but also to apply the data to the object.

If you simply run full_hydrate only once, then the found object will not have any of its fields updated.

@jarcoal
Copy link

jarcoal commented Aug 18, 2012

Having the same issue here when trying to b64decode an ImageField.

@tolomea
Copy link

tolomea commented Aug 18, 2012

Incidentally I worked around this like so:

def hydrate_password(self, bundle):
    # hash incoming password values
    if 'password' in bundle.data and not hasattr(bundle,

'password_hashed'):
bundle.password_hashed = True # guard against tastypie calling
this function multiple times
bundle.data['password'] = make_password(bundle.data['password'])
return bundle

On Sat, Aug 18, 2012 at 2:22 AM, jarcoal notifications@github.com wrote:

Having the same issue here when trying to b64decode an ImageField.


Reply to this email directly or view it on GitHubhttps://github.com/toastdriven/django-tastypie/issues/390#issuecomment-7840617.

@ghost
Copy link

ghost commented Aug 20, 2012

I did the same as @tolomea. I mark the bundle as processed (in my code) so that repeated calls to hydrate() know what's been done.

hydrate() gets called from several different paths and if you're overriding methods (obj_create, for eg) and then calling super within your implementation, it's more than likely both you and the super will be performing a hydrate().

So marking the bundle is the easiest way to work around this. Once you do this, it becomes a non-issue.

@JohnRandom
Copy link

I ended up doing the same thing. However, the documentation should probably reflect the fact, that hydrate_% methods can be called multiple times...

maraujop added a commit to TwoApart/django-tastypie that referenced this issue Sep 7, 2012
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
maraujop added a commit to TwoApart/django-tastypie that referenced this issue Sep 7, 2012
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
@toastdriven
Copy link
Contributor

This should be either fixed or at least better as of SHA: 213d686.

@ondrowan
Copy link
Contributor

ondrowan commented Oct 9, 2012

I suspect hydrate is now running twice. First in lookup_kwargs_with_identifiers on L1943 (hydrate) and then in full_hydrate on L1987. So, I ended up using @tolomea's workaround for now. Could anyone look into it?

buzzeante pushed a commit to TwoApart/django-tastypie that referenced this issue Jun 9, 2013
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
@sunshineo
Copy link

I am facing the same issue. And in the first call bundle.obj.pk is empty even though the request was an update on an existing resource. My hydrate method tries to use pk as an indicator if this is a create or update and want to do different hydrate in each case. But because of this bug, I'm screwed.
Why is this issue closed?

@fizyk
Copy link

fizyk commented Jul 30, 2013

I have same issue as sunshineo. Whenever i send PUT request, the hydrate process gets called twice, first time resulting in empty object, and only the second call have proper object loaded

@ondrowan
Copy link
Contributor

Could anyone reopen this issue? Seems like it isn't fixed and is referenced only from bunch of closed issues.

@linzhichu
Copy link

same issue here. I work around it by first testing if bundle is None at the beginning of hydrate method, but this should be fixed.

@trojkat
Copy link

trojkat commented Nov 14, 2013

Please reopen, this issue is still here (v0.10.0)

buzzeante pushed a commit to TwoApart/django-tastypie that referenced this issue Dec 7, 2013
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
@cjrd
Copy link

cjrd commented Jan 21, 2014

experiencing this problem in v0.11.0

@brandonkane
Copy link

We are also experiencing this on v0.11.0 with nested resources which are getting created twice if there is a child object created in a patch request. Has anyone figured out a workaround for this yet?

@letoss
Copy link

letoss commented Sep 16, 2016

Hello! I'm using 0.12.2 and I'm also experiencing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests