<stdimagefieldfile:none =“”>不是JSON可序列化的

时间:2016-05-22 13:35:07

标签: serialization django-models django-allauth django-serializer

我使用django-stdimage上传和调整图片大小,效果很好 但是,我和django-allauth有问题;当我尝试使用社交帐户登录,并且已经存在具有相同电子邮件地址的普通帐户时,我收到以下错误:

  

/ accounts / facebook / login / callback /
的TypeError    不是JSON可序列化的

这是完整的追溯:

Environment:


Request Method: GET
Request URL: http://localhost:8000/accounts/facebook/login/callback/?code=AQCf7MjgfOAsqf0sS0gup0hqLKyZClQvkGKyWtkORNBru_ITaRNHKgxwaH5RaCSARIb9U1ZgnqhWm3OQAfKW1r5nbVRkKr4fcLWtXdGL85-LYIyuF-NftkJpIhdIMR-VTMF8XXbKescZhxz0hDP_eKl1tKL6uPqWKc8NliWWHh9kOYSS69rAzNRUjZhgx6Zul9sAkV9nRoDo-JunhDRtvOV3crnpr9zAU6jsPDChcJ5dgcRPQ39EoOhrDE16-ia6WF1lFMz_fw1Pgjvo-2jduNG-c9TPyY23A205wm3d1PItoXH2U4GU8j1u5iAg1OIJuvDh-2viQA1disQoM_Du3vUldbX4Plun-yNay2kzNepOyw&state=0J6Ydn3lDKi0

Django Version: 1.8
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'blog',
 'custom_user',
 'django_markdown',
 'storages',
 'parsley',
 'stdimage',
 'stdimage_serializer',
 'rest_framework',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.facebook')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/allauth/socialaccount/providers/oauth2/views.py" in view
  62.                 return self.dispatch(request, *args, **kwargs)
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/allauth/socialaccount/providers/oauth2/views.py" in dispatch
  135.             return complete_social_login(request, login)
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/allauth/socialaccount/helpers.py" in complete_social_login
  145.         return _complete_social_login(request, sociallogin)
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/allauth/socialaccount/helpers.py" in _complete_social_login
  161.         ret = _process_signup(request, sociallogin)
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/allauth/socialaccount/helpers.py" in _process_signup
  26.         request.session['socialaccount_sociallogin'] = sociallogin.serialize()
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/allauth/socialaccount/models.py" in serialize
  198.                    user=serialize_instance(self.user),
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/allauth/utils.py" in serialize_instance
  194.     return json.loads(json.dumps(data, cls=DjangoJSONEncoder))
File "/usr/lib/python2.7/json/__init__.py" in dumps
  250.         sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py" in encode
  207.         chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py" in iterencode
  270.         return _iterencode(o, 0)
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/django/core/serializers/json.py" in default
  112.             return super(DjangoJSONEncoder, self).default(o)
File "/usr/lib/python2.7/json/encoder.py" in default
  184.         raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /accounts/facebook/login/callback/
Exception Value: <StdImageFieldFile: None> is not JSON serializable

我无法理解这是否是django-allauth问题或其他问题。
这是 models.py

class CustomUser(AbstractBaseUser, PermissionsMixin):
    first_name     = models.CharField(max_length=254, blank=True)
    second_name    = models.CharField(max_length=254, blank=True)
    email          = models.EmailField(blank=True, unique=True)
    date_joined    = models.DateTimeField(_('date joined'), default=datetime.now())
    #avatar            = models.ImageField('profile picture', upload_to=upload_avatar_to, null=True, blank=True)
    avatar         = StdImageField(upload_to=upload_avatar_to, null=True, blank=True,
                                   variations={
                                       'thumbnail': {'width': 250, 'height': 250, "crop": True}
                                   })
    is_active      = models.BooleanField(default=False)
    is_admin       = models.BooleanField(default=False)
    is_staff       = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'second_name']

    objects = CustomUserManager()

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def save(self, *args, **kwargs):
        super(CustomUser, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return "/users/%s" % urlquote(self.email)

    def get_full_name(self):
        """
        Returns the first_name plus the last_name, with a space in between.
        """
        return self.email

    def __str__(self):
        return self.email

    def get_short_name(self):
        """
        Returns the first name for the user.
        """ 
        return self.first_name

    def email_user(self, subject, message, from_email=None):
        """
        Sends an email to this user.
        """
        send_email(subject, message, from_email, [self.email])

我还试图使用django-stdimage-serializer,但是当我尝试使用它设置模型文件,并启动makemigrations时,我得到的是:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/stefano/projects/blog-project/blog/models.py", line 6, in <module>
    from custom_user.models import CustomUserManager, CustomUser
  File "/home/stefano/projects/blog-project/custom_user/models.py", line 52, in <module>
    class CustomUser(AbstractBaseUser, PermissionsMixin):
  File "/home/stefano/projects/blog-project/custom_user/models.py", line 60, in CustomUser
    'thumbnail': {'width': 250, 'height': 250, "crop": True}
  File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/rest_framework/fields.py", line 1405, in __init__
    super(ImageField, self).__init__(*args, **kwargs)
  File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/rest_framework/fields.py", line 1359, in __init__
    super(FileField, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'upload_to'

我该怎么办?

1 个答案:

答案 0 :(得分:1)

如果有人有兴趣,mark.js正是我所寻找的。