-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
fixing unique together validator for fields with source #7005
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
fixing unique together validator for fields with source #7005
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @anveshagarwal. Could you add a test case that demonstrates the failure?
Hi @rpkilby i added the testcase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @anveshagarwal. Please take this feedback constructively, but there is a lot wrong with the PR. I assume a lot of the issues below would have been caught, however, because test_read_only_default
doesn't contain an __init__.py
, it's not a proper python package. So, the test module isn't discovered and the tests aren't executed. This is why it's often suggested that you write your tests before the fix.
If you write the test first and see the failure, you know two things:
- the test was executed
- the test demonstrates the failure
If you fix the bug first then write the tests second, you can't guarantee the above.
@rpkilby thanks a lot for your valuable feedback. I learn a lot from it. I am pretty new in Development which led to these silly mistakes. This is my first error reporting to any open source. Will keep all the points you mentioned in my further approaches. And yes i had a time crunch to report these so i didnt approach it well enough. Thanks again. |
Of course, happy to provide feedback. |
@rpkilby i am not able to get the serializer class from which i can get the source name of the field |
hey @rpkilby as you can see in my changes i am getting serializer calss from |
…est for uniquetogether validator
need help on this. Want to resolve it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.djangoproject.com/en/dev/ref/models/options/#unique-together
Use UniqueConstraint with the constraints option instead.
UniqueConstraint provides more functionality than unique_together. unique_together may be deprecated in the future.
@auvipy but that is to be used in models? isnt it? How that will help me not to get |
@anveshagarwal The suggestion is about future-proofing the tests, not fixing the bug. i.e., if Also, I'm taking another stab at this, but am currently waiting on #7076 which is a followup to some validator changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rebase needed
Hi @anveshagarwal. I'm closing this in favor of #7086. Thanks for getting this started - you definitely found quite the obscure bug 😄 |
@rpkilby hey thanks! and i really wanted to contribute by having some commits in the DRF :) But all i am happy to contribute in anyway. And if i would have looked to the changes done after removing |
Description
As described in the issue below , the fields which are read_only + default are excluded from writable_fields and they are under _read_only_defaults function of serializer
https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L439
now in this function ,here in defaults orderdict() fields.field_name is set as key. So the problem is that if the field_name is different from the field in the model(with which the source keyword is filled), uniquetogether validator throws a
field is required error
as it is not able to map the source.refs##7003