Relax redundant source name restriction for Fields #9389
Unanswered
james-mchugh
asked this question in
Ideas & Suggestions
Replies: 1 comment 2 replies
-
If you really insist on having source in your custom field, how about changing your class NameField(serializers.CharField):
def __init__(
self,
max_length=128,
source="name",
**kwargs,
):
kwargs.setdefault("validators", [
validators.MinLengthValidator(
1, message="The name must contain at least one character."
),
validators.RegexValidator(
regex=r"[a-zA-Z-]+",
message="The name must only contain letters and dashes.",
),
])
super().__init__(max_length=max_length, source=source, **kwargs) Then, you can disable this assertion by passing class MySerializerV1(serializers.Serializer):
old_name = fields.NameField()
class MySerializerV2(serializers.Serializer):
name = fields.NameField(source=None) That also enables you to override other keyword arguments: class MySerializerV3(serializers.Serializer):
short_name = fields.NameField(max_length=100) Alternative solutionAlthough, personally, I would probably remove the class NameField(serializers.CharField):
def __init__(
self,
max_length=128,
**kwargs,
):
kwargs.setdefault("validators", [...])
super().__init__(max_length=max_length, **kwargs) In which case, the 2 serializers would look like this: class MySerializerV1(serializers.Serializer):
old_name = fields.NameField(source="name")
class MySerializerV2(serializers.Serializer):
name = fields.NameField() I find it more explicit and clearer in the first one where the Hope that helps. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Similar to as suggested and accepted in #2420, could the assertion for
source
be relaxed as well?In the use case I'm currently working, I have several serializers for different representations of the same underlying model. Since all of these serializers ultimately map to the the same model fields, they require a lot of the same validation logic. I've been wrestling with the best way of doing this, given that I can't really put that validation logic in the central location of model as discussed in #7850.
One of the ways I have landed on doing this was to create a common fields.py module that contains common DRF Fields with validation logic for each of the model fields. Below is an example:
This should solve many of the common validation issues I have, except for the fact that I cannot use it in a serializer if
source
is the same as the field name. Consider the dummy example below:Because the
source
argument is redundant, it triggers theAssertionError
at https://github.com/encode/django-rest-framework/blob/master/rest_framework/fields.py#L360 when runningbind
forMySerializerV2
.One of the other approaches I considered was putting all of this validation logic in my model and using a
ModelSerializer
, since DRF automatically pulls the validators from Django model fields. This is fine if my representations are an exact mapping to my model, but they're not. I attempted to work around this by creating a base class that inherits fromModelSerializer
and overridesget_fields
so child classes can redefine mappings between the representation fields and model fields. This worked, but I wasn't super happy with how the resulting code looked. I can share more details on this if interested, but I don't want to distract from my main question.Is there a better way of doing what I'm trying to do? Otherwise, what are your thoughts on relaxing the assertion around redundant sources?
Beta Was this translation helpful? Give feedback.
All reactions