简体   繁体   English

/ __init __()的Django Rest Framework TypeError为关键字参数'read_only'获取了多个值

[英]Django Rest Framework TypeError at / __init__() got multiple values for keyword argument 'read_only'

This super helpful error "ATypeError at / init () got multiple values for keyword argument 'read_only'" is occurring with the following serializers 以下序列化程序发生此超级有用的错误“ / init ()处的ATypeError为关键字参数'read_only'获取了多个值”

class UserSerializer(serializers.ModelSerializer):
    posts = serializers.HyperlinkedIdentityField('posts', lookup_field='username', read_only=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'first_name', 'last_name', 'posts', )


class PostSerializer(serializers.ModelSerializer):
    author = UserSerializer(required=False,  read_only=True)
    photos = serializers.HyperlinkedIdentityField('photos', read_only=True)
    # author = serializers.HyperlinkedRelatedField(view_name='user-detail', lookup_field='username')

    def get_validation_exclusions(self, *args, **kwargs):
        # Need to exclude `user` since we'll add that later based off the request
        exclusions = super(PostSerializer, self).get_validation_exclusions(*args, **kwargs)
        return exclusions + ['author']

    class Meta:
        model = Post


class PhotoSerializer(serializers.ModelSerializer):
    image = serializers.Field('image.url' , read_only=True)


    class Meta:
        model = Photo

Any help please? 有什么帮助吗?

The field HyperlinkedIdentityField is always read-only , that is why it doesn't support read_only argument. 字段HyperlinkedIdentityField 始终为只读字段,这就是为什么它不支持read_only参数的原因。 Allowed arguments: 允许的参数:

  • view_name view_name
  • lookup_field lookup_field
  • lookup_url_kwarg lookup_url_kwarg
  • format 格式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:__init __()为关键字参数“ choices”获得了多个值 - TypeError: __init__() got multiple values for keyword argument 'choices' TypeError:“ __ init __()为关键字参数'name'获得了多个值” - TypeError: “__init__() got multiple values for keyword argument 'name'” TypeError:__ init __()得到关键字参数'customer'的多个值 - TypeError: __init__() got multiple values for keyword argument 'customer' Django TypeError: __init__() 得到了一个意外的关键字参数“attrs” - Django TypeError: __init__() got an unexpected keyword argument 'attrs' Django 错误:TypeError:__init__() 得到了一个意外的关键字参数“attrs” - Django Error: TypeError: __init__() got an unexpected keyword argument 'attrs' Django rest framework / simplejson错误__init __()得到了一个意外的关键字参数 - Django rest framework/simplejson error __init__() got an unexpected keyword argument 在Django Rest Framework中使用get调用时,__init __()获得了意外的关键字参数'context' - __init__() got an unexpected keyword argument 'context' when calling with get in Django Rest Framework 在Scrapy中使用规则和请求会引发异常TypeError:__init __()为关键字参数'callback'获得了多个值 - Using Rules and Requests in Scrapy throws exception TypeError: __init__() got multiple values for keyword argument 'callback' TypeError at '' __init__() 得到一个意外的关键字参数 '' - TypeError at '' __init__() got an unexpected keyword argument '' windrose:__init __()获得了多个关键字参数值 - windrose: __init__() got multiple values for keyword argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM