django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

Add support for source with attributes in extra_kwargs of ModelSerializer

Open BergLucas opened this issue 2 years ago • 8 comments

refs #4688

Description

Hello dear maintainers, it's my first contribution to django rest framework so I hope I didn't do anything wrong.

In the pull request referenced above, there is someone that mentioned that we could not use source with attributes in extra_kwargs of a ModelSerializer.

For example, the following code would create an error:

class MyUser(models.Model):

    user = models.OneToOneField(User, on_delete=models.DO_NOTHING)

class MyUserSerializer(serializers.ModelSerializer):

    class Meta:
        model = MyUser
        fields = (
            "username",
        )
        extra_kwargs = {
            "username": {"source": "user.username"},
        }

I think it could be a very interesting feature because at the moment, when we have a foreign key to another model, we're obliged to specify the fields explicitly in the serializer. However, this means that if we have special validators on the fields of our model, we're obliged to put them back on the serializer fields.

For example, the username field of Django's default User has a special validator so if we just define a basic CharField, it would not validate the data the same way as the model would validate it:

class MyUser(models.Model):

    user = models.OneToOneField(User, on_delete=models.DO_NOTHING)

class MyUserSerializer(serializers.ModelSerializer):

    username = serializers.CharField(source="user.username")

    class Meta:
        model = MyUser
        fields = (
            "username",
        )

This could create a difference between the way the model validates data and the way the serializer validates data if we forgot a validator or if we change the model without changing the serializer.

In this pull request, I added this feature so that we could just specify the model field we want in the source of extra_kwargs and it will generate the right field on the serializer.

The code may seem a little odd, but I've tried to keep the changes in one place only. I've also tried to maintain a good error message so that, if at any point the path to the field is wrong, it returns the full path in the error message and not just part of it.

Thanks for reading and please let me know if there are any changes that could be made.

BergLucas avatar Aug 18 '23 07:08 BergLucas

___________________________________ summary ____________________________________ ERROR: py36-django30: commands failed py36-django31: commands succeeded py36-django32: commands succeeded

auvipy avatar Sep 14 '23 13:09 auvipy

Hi @auvipy,

And this should also require documentation update as far as I can understand!

Do you mean to add a section here (or elsewhere) to document the feature?

BergLucas avatar Sep 14 '23 16:09 BergLucas

Hi @auvipy,

And this should also require documentation update as far as I can understand!

Do you mean to add a section here (or elsewhere) to document the feature?

yup there or somewhere else where it would be relevant

auvipy avatar Oct 02 '23 05:10 auvipy