django-allauth: is not JSON serializable
Hello!
I have Django==1.6.1 and django-allauth==0.15.0.
I use custom user model
class User(AbstractBaseUser, PermissionsMixin):
name = models.CharField('Name', max_length=255)
email = models.EmailField(verbose_name="E-Mail", max_length=255, unique=True, db_index=True)
photo = StdImageField(verbose_name="Photo", upload_to="photos", blank=True, size=(290, 290), thumbnail_size=(90, 90, True))
...
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["name", ]
After sign in with Twitter I got
TypeError at /accounts/twitter/login/callback/
<ImageFieldFile: None> is not JSON serializable
Traceback
Traceback:
File "/home/www/.virtualenvs/site/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/www/.virtualenvs/site/lib/python2.7/site-packages/allauth/socialaccount/providers/oauth/views.py" in view
35. return self.dispatch(request, *args, **kwargs)
File "/home/www/.virtualenvs/site/lib/python2.7/site-packages/allauth/socialaccount/providers/oauth/views.py" in dispatch
91. return complete_social_login(request, login)
File "/home/www/.virtualenvs/site/lib/python2.7/site-packages/allauth/socialaccount/helpers.py" in complete_social_login
119. return _complete_social_login(request, sociallogin)
File "/home/www/.virtualenvs/site/lib/python2.7/site-packages/allauth/socialaccount/helpers.py" in _complete_social_login
130. ret = _process_signup(request, sociallogin)
File "/home/www/.virtualenvs/site/lib/python2.7/site-packages/allauth/socialaccount/helpers.py" in _process_signup
28. request.session['socialaccount_sociallogin'] = sociallogin.serialize()
File "/home/www/.virtualenvs/site/lib/python2.7/site-packages/allauth/socialaccount/models.py" in serialize
166. user=serialize_instance(self.account.user),
File "/home/www/.virtualenvs/site/lib/python2.7/site-packages/allauth/utils.py" in serialize_instance
151. return json.loads(json.dumps(ret, cls=DjangoJSONEncoder))
File "/opt/python2.7/lib/python2.7/json/__init__.py" in dumps
250. sort_keys=sort_keys, **kw).encode(obj)
File "/opt/python2.7/lib/python2.7/json/encoder.py" in encode
207. chunks = self.iterencode(o, _one_shot=True)
File "/opt/python2.7/lib/python2.7/json/encoder.py" in iterencode
270. return _iterencode(o, 0)
File "/home/www/.virtualenvs/site/lib/python2.7/site-packages/django/core/serializers/json.py" in default
104. return super(DjangoJSONEncoder, self).default(o)
File "/opt/python2.7/lib/python2.7/json/encoder.py" in default
184. raise TypeError(repr(o) + " is not JSON serializable")
Exception Type: TypeError at /accounts/twitter/login/callback/
Exception Value: <ImageFieldFile: None> is not JSON serializable
Advice with SESSION_SERIALIZER dont help me. Result is the same.
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 24 (5 by maintainers)
This is a django-allauth issue because custom user models can have any Django model field. If your user model had a
HandFieldfrom the docs, for example, this error would occur becauseDjangoJSONEncoderdoesn’t know how to encode that type.Potential solutions include:
Userinstance. Instead just serialise the natural key and fetch the user using that when needed (seems best to me)you can’t serialize the object, because it’s an Image. You have to serialize the string representation of it’s path. The easiest way of achiving it is to call it’s str() method when you what to serialize it. json.dumps(unicode(my_imagefield)) #py2 json.dumps(str(my_imagefield)) #py3 should work.
For sharing. I ended up adding a validation in the deserialize_instance method
I copied the original method and added this line in previous if:
and not isinstance(f, ImageField)Thx.
I’ve moved ahead and added the above (cedad9f156a8c78bfbe43a0b3a723c1a0b840dbd). This approach is not pretty, but hopefully it is effective. Could you all let me know if this solves the issues with your custom user models…?
I fixed it changing the name of the fields, but I also added a proxy to the original name of the fields, so I could keep the rest of the code as it was.
Before the fix.
With the fix.