david
david

Reputation: 6795

Why are two arguments provided when I call a parent class method in Python

I am trying to call the cleanse method from ParentSerializer but from the ChildSerializer as seen in the last line.

class ParentSerializer(models.ModelSerializer):
    def cleanse(self):
        if hasattr(self, 'initial_data'):
            val1 = self.initial_data['name']
            val2 = self.initial_data['age']
            val3 = self.initial_data['city']
        return self.initial_data

class ChildSerializer(GenericSerializer):
    def is_valid(self, raise_exception=False):
        if hasattr(self, 'initial_data'):
            super(ChildSerializer, self).cleanse(self)

error:

TypeError: cleanse() takes 1 positional argument but 2 were given

Why is this not working?

Upvotes: 0

Views: 32

Answers (1)

Primusa
Primusa

Reputation: 13498

Replace your final line with

super(ChildSerializer, self).cleanse()

You don't need self, super call refers to your parent class and acts as it.

Upvotes: 1

Related Questions