Skip to content
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

Closed

Conversation

anveshagarwal
Copy link

@anveshagarwal anveshagarwal commented Oct 18, 2019

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

Copy link
Member

@rpkilby rpkilby left a 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?

@anveshagarwal
Copy link
Author

Hi @rpkilby i added the testcase

Copy link
Member

@rpkilby rpkilby left a 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.

rest_framework/serializers.py Outdated Show resolved Hide resolved
test_read_only_default/test_read_only_default.py Outdated Show resolved Hide resolved
test_read_only_default/test_read_only_default.py Outdated Show resolved Hide resolved
test_read_only_default/test_read_only_default.py Outdated Show resolved Hide resolved
test_read_only_default/test_read_only_default.py Outdated Show resolved Hide resolved
test_read_only_default/test_read_only_default.py Outdated Show resolved Hide resolved
test_read_only_default/test_read_only_default.py Outdated Show resolved Hide resolved
test_read_only_default/test_read_only_default.py Outdated Show resolved Hide resolved
@anveshagarwal
Copy link
Author

anveshagarwal commented Oct 23, 2019

@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.

@rpkilby
Copy link
Member

rpkilby commented Oct 23, 2019

Of course, happy to provide feedback.

@anveshagarwal
Copy link
Author

@rpkilby i am not able to get the serializer class from which i can get the source name of the field

@anveshagarwal
Copy link
Author

hey @rpkilby as you can see in my changes i am getting serializer calss from set_context function but in the only test failing filter_queryset is called directly so there i cant get the serializer class. But in the implementation set_context is always called first. So these changes are working(solves bugs) in my existing work but test is failing here.

@anveshagarwal anveshagarwal changed the title fixing the source from where the name of the field should be taken fixing unique together validator Oct 29, 2019
@anveshagarwal anveshagarwal changed the title fixing unique together validator fixing unique together validator for fields with source Oct 29, 2019
@anveshagarwal
Copy link
Author

need help on this. Want to resolve it.

Copy link
Member

@auvipy auvipy left a 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.

@anveshagarwal
Copy link
Author

@auvipy but that is to be used in models? isnt it? How that will help me not to get this bug in serializer's uniquetogethervalidatior? How will i get serializer level validation without this bug from solution you suggested? Wont this bug will occur if i use UniqueConstraint with the constraints option instead?

@rpkilby
Copy link
Member

rpkilby commented Dec 5, 2019

@anveshagarwal The suggestion is about future-proofing the tests, not fixing the bug. i.e., if UniqueConstraint is preferred over Meta.unique_together, with the latter potentially being deprecated, it would make sense to go ahead and use the constraints option. That said, UniqueConstraint was only added in Django 2.2, so we're not able to use it yet since we still support Django 1.11

Also, I'm taking another stab at this, but am currently waiting on #7076 which is a followup to some validator changes.

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

rebase needed

@rpkilby
Copy link
Member

rpkilby commented Dec 11, 2019

Hi @anveshagarwal. I'm closing this in favor of #7086. Thanks for getting this started - you definitely found quite the obscure bug 😄

@rpkilby rpkilby closed this Dec 11, 2019
@anveshagarwal
Copy link
Author

@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 set_context implementation ,I might have solved the part i was stuck on :)

Sign up for free to 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.

None yet

4 participants