-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Closed
anveshagarwal
wants to merge
9
commits into
encode:master
from
anveshagarwal:read_default_source_field_fix
Closed
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
09d028d
fixing the source from where the name of the field should be taken
6d427b4
test case that is failing
0cc4624
choice of both field.source and field.field_name added
aa500b3
1.Revoked the previous change fixed the uniquetogether validator
e6ad108
update operation in uniquetogether validator is fixed
c87e53a
fixing for PUT requests
a3e5559
added proper tests for the readonly and writable fields with source t…
5902dac
fixing writable fields test by including field.field_name in place of…
e240a63
reverting changes for writable field fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from django.db import models | ||
from rest_framework import serializers | ||
from rest_framework.validators import ( | ||
UniqueTogetherValidator, | ||
) | ||
|
||
from rest_framework import viewsets | ||
import json | ||
rpkilby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from rest_framework import mixins | ||
from rest_framework import generics | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
from django.test import TestCase | ||
from rest_framework import status | ||
|
||
|
||
class ModelTwo(models.Model): | ||
testfieldtwo = models.CharField(max_length=60, primary_key=True) | ||
|
||
class ModelOne(models.Model): | ||
testfield = models.ForeignKey(ModelTwo, on_delete=models.CASCADE) | ||
alias = models.CharField(max_length=60) | ||
name = models.CharField(max_length=60) | ||
rpkilby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class Meta: | ||
unique_together = ('testfield', 'name',) | ||
rpkilby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class ExampleSerializer(serializers.ModelSerializer): | ||
altername = serializers.CharField(source="testfield", default=ModelTwo(testfieldtwo="model 2 default"), read_only=True) | ||
rpkilby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class Meta: | ||
model = ModelOne | ||
fields = ('altername' ,'alias', 'name') | ||
validators = [ | ||
UniqueTogetherValidator( | ||
queryset=ModelOne.objects.all(), | ||
fields=('testfield', 'name') | ||
) | ||
] | ||
|
||
class ExampleViewSet(mixins.CreateModelMixin, | ||
mixins.UpdateModelMixin, | ||
viewsets.ReadOnlyModelViewSet): | ||
|
||
queryset = ModelOne.objects.all() | ||
serializer_class = ExampleSerializer | ||
rpkilby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class ExampleTests(TestCase): | ||
|
||
def url(self): | ||
return '/example/' | ||
rpkilby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_created_successfully(self): | ||
name = 'blab blah' | ||
alias = 'ab ab' | ||
response = self.client.post(self.url, {'name': name, 'alias': alias}) | ||
rpkilby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.conf.urls import include, url | ||
from rest_framework import routers | ||
from test_read_only_default import ExampleViewSet | ||
|
||
router = routers.DefaultRouter() | ||
router.register(r'example', ExampleViewSet) | ||
|
||
urlpatterns = [ | ||
url('', include(router.urls)), | ||
url('api-auth/', include('rest_framework.urls', namespace='rest_framework')) | ||
] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.