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

TypeError: set_context() missing 1 required positional argument: 'serializer_field' #6755

Closed
6 tasks
kdzxiaoli opened this issue Jun 24, 2019 · 3 comments
Closed
6 tasks

Comments

@kdzxiaoli
Copy link

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

class GraphDomainSerializer(serializers.ModelSerializer):
user = serializers.HiddenField(default=serializers.CurrentUserDefault)

Expected behavior

Actual behavior

class CurrentUserDefault(object):
def set_context(self, serializer_field):
self.user = serializer_field.context['request'].user

def __call__(self):
    return self.user

def __repr__(self):
    return unicode_to_repr('%s()' % self.__class__.__name__)

def get_default(self):
"""
Return the default value to use when validating data if no input
is provided for this field.

    If a default has not been set for this field then this will simply
    raise `SkipField`, indicating that no value should be set in the
    validated data for this field.
    """
    if self.default is empty or getattr(self.root, 'partial', False):
        # No default, or this is a partial update.
        raise SkipField()
    if callable(self.default):
        if hasattr(self.default, 'set_context'):
            **self.default.set_context(self)**
        return self.default()
    return self.default

File "/Users/lishijin/.pyenv/versions/3.7.2/lib/python3.7/site-packages/rest_framework/fields.py", line 490, in get_default
self.default.set_context(self)
TypeError: set_context() missing 1 required positional argument: 'serializer_field'
[24/Jun/2019 14:34:45] "POST /graphs/ HTTP/1.1" 500 21130

@xordoquy
Copy link
Collaborator

The default value should be an instance, not a class: serializers.CurrentUserDefault() as show in the documentation

@Liasota
Copy link

Liasota commented Aug 21, 2019

Значением по умолчанию должен быть экземпляр, а не класс: serializers.CurrentUserDefault()как показано в документации

it solved the problem

@MH-Blog
Copy link

MH-Blog commented Jan 16, 2020

The default value should be an instance, not a class: serializers.CurrentUserDefault() as show in the documentation

Thanks,You helped me solve this problem

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

4 participants