我用Django和DRF编写了以下代码:
class ServiceModelMetaClass(serializers.SerializerMetaclass, type):
SERVICE_MODELS = {
"email": EmailSubscriber,
"push": PushSubscriber
}
def __call__(cls, *args, **kwargs):
service = kwargs.get("data", {}).get("service")
cls.Meta.subscriber_model = cls.SERVICE_MODELS.get(service)
return super().__call__(*args, **kwargs)
class InterListsActionsSerializer(serializers.Serializer, metaclass=ServiceModelMetaClass):
source_list_id = serializers.IntegerField()
target_list_id = serializers.IntegerField()
subscriber_ids = serializers.IntegerField(many=True, required=False)
account_id = serializers.CharField()
service = serializers.ChoiceField(choices=("email", "push"))
class Meta:
subscriber_model: Model = None
def move(self):
model = self.Meta.subscriber_model
# Rest of the method code.
The purpose of this code is that this serializer might need doing operation on different models based on the service that the user wants to use. So I wrote this metaclass to prevent writing duplicate code and simply change the subscriber_model
based on user's needs.
Now as you might know, serializers.Serializer
uses a metaclass by its own, serializers.SerializerMetaclass
. If I don't use this metaclass for creating my metaclass, it results in the following error:
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases.
But when I try making my ServiceModelMetaClass
metaclass to inherit from serializers.SerializerMetaclass
it gives me this error:
File "/project-root/segment_management/serializers/subscriber_list.py", line 33, in <module>
class InterListsActionsSerializer(serializers.Serializer, metaclass=ServiceModelMetaClass):
File "/project-root/segment_management/serializers/subscriber_list.py", line 36, in InterListsActionsSerializer
subscriber_ids = serializers.IntegerField(many=True, required=False)
File "/project-root/.venv/lib/python3.10/site-packages/rest_framework/fields.py", line 894, in __init__
super().__init__(**kwargs)
TypeError: Field.__init__() got an unexpected keyword argument 'many'
我应该做什么来解决这个问题,或者是一个更好的替代方法,在不使用元类的情况下保持代码清洁?先谢谢你.