user7693832
user7693832

Reputation: 6849

Can I realize the exclude fields function in Serializer?

In my serializers.py, I created a UserListSerializer:

class UserListSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'
        exclude = [
            'password',
        ]

I want to realize the exclude fields function, but failed.

AssertionError at /api/users/list/
Cannot set both 'fields' and 'exclude' options on serializer UserListSerializer.

Is it possible to realize this function, because the fields is too much?

Upvotes: 0

Views: 2828

Answers (1)

Exprator
Exprator

Reputation: 27503

you cannot use both fields and exclude in the Meta Class of your serializer

instead try this

class UserListSerializer(ModelSerializer):
    class Meta:
        model = User
        exclude = [
            'password',
        ]

Upvotes: 3

Related Questions