Django-rest-framework: TypeError: set_context() missing 1 required positional argument: 'serializer_field'

Created on 24 Jun 2019  ·  3Comments  ·  Source: encode/django-rest-framework

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

Most helpful comment

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

All 3 comments

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

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

it solved the problem

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

Thanks,You helped me solve this problem

Was this page helpful?
0 / 5 - 0 ratings