-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validator for serializer field with source=
kwargs
#3844
Comments
Oh, seems I've forgot to add regression test. Let's first discuss on this. If this is right, I'll add the necessary regression test. |
Hi, looks like it's an issue in your code since you're not serializing the Customer instance. |
@xordoquy I think it's not an issue of my code. Actually, I've changed the code as I posted here, and all tests passed. The logic for the code:
|
If you think it's really a DRF bug the next step would be to write a failing test case we could discuss. |
I have the same issue. It only occurs when implementing the UniqueValidator. Any ideas of resolving without hardcode drf¡ |
@Cesaringo Perhaps cherry-pick my closed rejected PR? |
@Cesaringo Spent two hours, added a regression testcase, take some time to help me review this patch? |
…r-with-related-field Fix #3844, refine validator for fields with <source=> kwargs
@Cesaringo @xiaohanyu Is this intended behaviour ? I think it shouldn't be, but I'm looking forward to hear from you.
Say we have a Thanks. |
I've drafted a API with DRF, and there're two models,
django.contrib.auth.models.User
andCustomer
, code forCustomer
model:Note that
Customer
has auser = models.OneToOneField(User, on_delete=models.CASCADE)
field, thus we can still use django's built authentication while add some extra field to our ownCustomer
model.The code for
CustomerSerializer
:But when I work with
CustomerSerializer
, it will raise an exception:I've checked DRF's code, and I think this exception comes from https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/validators.py#L38
I've read DRF's documentation: http://www.django-rest-framework.org/api-guide/fields/, it says
And for
EmailField(source='user.email')
, the codeself.field_name = serializer_field.source_attrs[0]
will returnuser
instead ofemail
, which is not right and will lead to aFieldError
.For my case,
I think it's better to change
self.field_name = serializer_field.source_attrs[0]
toself.field_name = serializer_field.source_attrs[-1]
.The text was updated successfully, but these errors were encountered: