Skip to content

Conversation

@BergLucas
Copy link

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:

classMyUser(models.Model): user=models.OneToOneField(User, on_delete=models.DO_NOTHING) classMyUserSerializer(serializers.ModelSerializer): classMeta: model=MyUserfields= ( "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:

classMyUser(models.Model): user=models.OneToOneField(User, on_delete=models.DO_NOTHING) classMyUserSerializer(serializers.ModelSerializer): username=serializers.CharField(source="user.username") classMeta: model=MyUserfields= ( "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.

@auvipyauvipy self-requested a review August 19, 2023 15:05
Copy link
Collaborator

@auvipyauvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add proper test cases to validate the changes proposed?

@auvipyauvipy requested review from a team and auvipyAugust 23, 2023 14:25
@auvipy
Copy link
Collaborator

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

Copy link
Collaborator

@auvipyauvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was a nice example

@auvipyauvipy added this to the 3.15 milestone Sep 14, 2023
Copy link
Collaborator

@auvipyauvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@BergLucasBergLucasforce-pushed the improvement/source-attributes-in-extra_kwargs branch from b10dfc2 to c1ccc37CompareSeptember 14, 2023 15:36
@BergLucas
Copy link
Author

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?

@auvipy
Copy link
Collaborator

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

@auvipyauvipy requested a review from a teamOctober 2, 2023 05:52
Copy link
Collaborator

@auvipyauvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this change conform with the principle of adding compatibility with django? I'm not fully convinced about the merit of the new change

@auvipyauvipy removed this from the 3.15 milestone Apr 27, 2025
@auvipyauvipy requested a review from a teamApril 28, 2025 03:26
@stale
Copy link

stalebot commented Jun 27, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stalestalebot added the stale label Jun 27, 2025
@ulgens
Copy link
Contributor

I think this is still relevant and shouldn't be marked as stale.

Comment on lines 1114 to 1115
ifattrnotinattr_info.relations:
break
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to cause trailing attributes to be ignored if they are "after" other relation attributes, but are not a relation themselves. Is this intended?

If so, I think this scenario should also have a test case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not really intended. Is it possible to have attributes other than relations with sufficient information in this function to be able to follow them? As far as I know, this isn't the case, so I just did it this way. It's also possible that I misunderstood your comment

@ulgens
Copy link
Contributor

@auvipy Do you know why the bot doesn't remove stale label when there is a new comment?

@ulgens
Copy link
Contributor

@BergLucas Hey 👋🏻 Is it possible for you to rebase this branch from the latest main, so it won't be closed as stale and we can push for another tour of review & testing? Thanks in advance.

@stalestalebot removed the stale label Jul 18, 2025
@BergLucas
Copy link
Author

Hi @ulgens ,

Thanks for your reviews! I've rebased the branch and responded to the comments.

(Sorry for the delay, I've been quite busy these last few weeks.)

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@BergLucas@auvipy@ulgens@peterthomassen