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

Hyperlink Class Cannot Be Pickled #3628

Closed
mlavin opened this issue Nov 12, 2015 · 7 comments
Closed

Hyperlink Class Cannot Be Pickled #3628

mlavin opened this issue Nov 12, 2015 · 7 comments
Labels
Milestone

Comments

@mlavin
Copy link

mlavin commented Nov 12, 2015

Related to #3350, the rest_framework.relations.Hyperlink instances cannot be pickled as demonstrated with this sample:

>>> import pickle
>>> from rest_framework.relations import Hyperlink
>>> link = Hyperlink('http://example.com', 'test')
>>> pickle.loads(pickle.dumps(link))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: __new__() missing 1 required positional argument: 'name'

For serializers which use the hyperlink relations, this breaks the caching of the response objects since most of the cache backends rely on pickle by default.

@xordoquy
Copy link
Collaborator

Thanks a lot @mlavin. I think this can be linked to another issue where cache was failing with a similar message though we were not able to identify where it did come from.

lsanpablo added a commit to lsanpablo/django-rest-framework that referenced this issue Dec 3, 2015
@lsanpablo
Copy link
Contributor

Hi @xordoquy, I would like to start contributing. Can I try this issue? I have solved @mlavin 's specific case by implementing the getnewargs() method https://docs.python.org/3.1/library/pickle.html#pickle.object.__getnewargs__ in lsanpablo@44a3d41 , but I've yet to write a test case.

@xordoquy
Copy link
Collaborator

xordoquy commented Dec 3, 2015

Hi @Cheglader, thanks for working at it. Looking forward to your pull request :)

@glynjackson
Copy link

Same issue using HyperlinkedIdentityField +1

@jpadilla
Copy link
Member

jpadilla commented Dec 6, 2015

I'd be great if anyone having this issue could give @Cheglader's PR #3701 a try.

@glynjackson
Copy link

This is my example code that fails.

class TestSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name='detail')

Error:

TypeError at /v1/test
__new__() takes exactly 3 arguments (2 given)
Request Method: GET
Request URL:    http://127.0.0.1:8000/v1/vendor
Django Version: 1.8.4
Exception Type: TypeError
Exception Value:    
__new__() takes exactly 3 arguments (2 given)
Exception Location: /Users/glyn/Documents/workspace/shortbite/django-env/lib/python2.7/site-packages/django/core/cache/backends/locmem.py in get, line 54
Python Executable:  /Users/glyn/Documents/workspace/shortbite/django-env/bin/python
Python Version: 2.7.8

Adding the following fixes the issue for me:

def __getnewargs__(self):
        return(str(self), self.name,)

@glynjackson
Copy link

@jpadilla yep @Cheglader's PR #3701 works for me! I'm using memcache and this has been a real pain. Any idea if/when the PR will be accepted?

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

No branches or pull requests

6 participants