Weird behavior with Django REST Serializer instantiation

I’m facing a weird issue with DRF serializers that I haven’t really yet been able to identify the cause of.

Here’s what I’ve been working on: I have several serializers for which I want to display certain fields only under specific conditions, such as url parameters there being in the request or the user having certain permissions. To give you a practical example—it’s a school app, and there’s an Exercise model with a solution field that’s not to be shown to students during a quiz.

To decouple my serializer’s presentation logic from the business logic, I decided to add a conditional_fields attribute to the serializer’s Meta class: it’s a dict in which the keys are strings representing conditions, such as "SHOW_HIDDEN_FIELDS" and their values are lists of field names that need to be removed if the key isn’t present in the serializer context. Then I override my viewsets’ get_serializer_context method to get the desired values inside of context.

I made a remove_unsatisfied_condition_fields method which does the following:

def remove_unsatisfied_condition_fields(self):
        conditional_fields = self.Meta.conditional_fields

        for condition, fields in conditional_fields.items():
            if not self.context.get(condition, False):
                for field in fields:
                    self.fields.pop(field, None)

The serializers making use of it look like this:

class ExerciseSerializer(serializers.ModelSerializer):
    class Meta:
        model = Exercise
        fields = [
            "id",
            "text",
            "solution",
            "correct_choices"
        ]

        conditional_fields = {
            "EXERCISE_SHOW_HIDDEN_FIELDS": ["solution", "correct_choices"],
        }

Here comes the problem: if I manually call this method inside my serializers, in their __init__ method, like this:

def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.remove_unsatisfied_condition_fields()

everything works fine.

However, if I make them inherit from a class that does it automatically, like this:

class ConditionalFieldsMixin:
    def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
         self.remove_unsatisfied_condition_fields()

    def remove_unsatisfied_condition_fields(self):
        conditional_fields = self.Meta.conditional_fields

        for condition, fields in conditional_fields.items():
            if not self.context.get(condition, False):
                for field in fields:
                    self.fields.pop(field, None)

and make my serializers inherit from it, the following happens:

as soon as a request comes in that doesn’t satisfy one of the serializer’s conditional_fields condition, the field appears to be “permanently” removed from the serializer: any subsequent requests that involve it, even if they lead to having a context with the proper conditions to show that field, are responded to without that field. It is as if the serializer ceases to have that field forever—only redeploying my application makes it come back.

This is very weird and I have no idea why this only happens if the removal is done inside of a class in the inheritance chain of the serializer vs doing it in the serializer itself.

Does this have to do with some weird Pythonic inheritance rule I’m not aware of or am I missing something about serializers?