From 8842e5fd97720810373e5c0818a282b0a571bd3e Mon Sep 17 00:00:00 2001 From: ByK95 Date: Sun, 27 Aug 2023 14:26:15 +0300 Subject: [PATCH] Add custom_many_related_field to RelatedField --- rest_framework/relations.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 4409bce77..f377c51ea 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -92,6 +92,7 @@ class RelatedField(Field): queryset = None html_cutoff = None html_cutoff_text = None + custom_many_related_field = None def __init__(self, **kwargs): self.queryset = kwargs.pop('queryset', self.queryset) @@ -137,16 +138,19 @@ class RelatedField(Field): and child classes in order to try to cover the general case. If you're overriding this method you'll probably want something much simpler, eg: - @classmethod - def many_init(cls, *args, **kwargs): - kwargs['child'] = cls() - return CustomManyRelatedField(*args, **kwargs) + ex: + + class SafePrimaryKeyRelatedField(PrimaryKeyRelatedField): + custom_many_related_field = CustomManyRelatedField + + will override ManyRelatedField with CustomManyRelatedField """ + many_related_field_cls = cls.custom_many_related_field or ManyRelatedField list_kwargs = {'child_relation': cls(*args, **kwargs)} for key in kwargs: if key in MANY_RELATION_KWARGS: list_kwargs[key] = kwargs[key] - return ManyRelatedField(**list_kwargs) + return many_related_field_cls(**list_kwargs) def run_validation(self, data=empty): # We force empty strings to None values for relational fields.