Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite2a4559

Browse files
saadullahaleemderonnaxMehrazRummantheomegajornvanwier
authored
Fix validation for ListSerializer (#8979)
* fix: Make the instance variable of child serializer point to the correct list object instead of the entire list when validating ListSerializer* fix formatting issues for list serializer validation fix* fix imports sorting for list serializer tests* remove django 2.2 from docs index (#8982)* Declared Django 4.2 support in README.md (#8985)* Fix Links in Documentation to Django `reverse` and `reverse_lazy` (#8986)* Fix Django Docs url in reverse.mdDjango URLs of the documentation of `reverse` and `reverse_lazy` were wrong.* Update reverse.md* fix URLPathVersioning reverse fallback (#7247)* fix URLPathVersioning reverse fallback* add test for URLPathVersioning reverse fallback* Update tests/test_versioning.py---------Co-authored-by: Jorn van Wier <jorn.van.wier@thunderbyte.ai>Co-authored-by: Asif Saif Uddin <auvipy@gmail.com>* Make set_value a method within `Serializer` (#8001)* Make set_value a static method for SerializersAs an alternative to#7671, let the method be overridden if needed. Asthe function is only used for serializers, it has a better place in theSerializer class.* Set `set_value` as an object (non-static) method* Add tests for set_value()These tests follow the examples given in the method.* fix: Make the instance variable of child serializer point to the correct list object instead of the entire list when validating ListSerializer* Make set_value a method within `Serializer` (#8001)* Make set_value a static method for SerializersAs an alternative to#7671, let the method be overridden if needed. Asthe function is only used for serializers, it has a better place in theSerializer class.* Set `set_value` as an object (non-static) method* Add tests for set_value()These tests follow the examples given in the method.* fix: Make the instance variable of child serializer point to the correct list object instead of the entire list when validating ListSerializer* fix: Make the instance variable of child serializer point to the correct list object instead of the entire list when validating ListSerializer* fix formatting issues for list serializer validation fix* fix: Make the instance variable of child serializer point to the correct list object instead of the entire list when validating ListSerializer* fix formatting issues for list serializer validation fix* fix linting* Update rest_framework/serializers.pyCo-authored-by: Sergei Shishov <sshishov@users.noreply.github.com>* Update rest_framework/serializers.pyCo-authored-by: Sergei Shishov <sshishov@users.noreply.github.com>* fix: instance variable in list serializer, remove commented code---------Co-authored-by: Mathieu Dupuy <deronnax@gmail.com>Co-authored-by: Mehraz Hossain Rumman <59512321+MehrazRumman@users.noreply.github.com>Co-authored-by: Dominik Bruhn <dominik@dbruhn.de>Co-authored-by: jornvanwier <mail@jornvanwier.com>Co-authored-by: Jorn van Wier <jorn.van.wier@thunderbyte.ai>Co-authored-by: Asif Saif Uddin <auvipy@gmail.com>Co-authored-by: Étienne Beaulé <beauleetienne0@gmail.com>Co-authored-by: Sergei Shishov <sshishov@users.noreply.github.com>
1 parentd252d22 commite2a4559

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

‎rest_framework/serializers.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,12 @@ def __init__(self, *args, **kwargs):
609609
self.min_length=kwargs.pop('min_length',None)
610610
assertself.childisnotNone,'`child` is a required argument.'
611611
assertnotinspect.isclass(self.child),'`child` has not been instantiated.'
612+
613+
instance=kwargs.get('instance', [])
614+
data=kwargs.get('data', [])
615+
ifinstanceanddata:
616+
assertlen(data)==len(instance),'Data and instance should have same length'
617+
612618
super().__init__(*args,**kwargs)
613619
self.child.bind(field_name='',parent=self)
614620

@@ -683,7 +689,13 @@ def to_internal_value(self, data):
683689
ret= []
684690
errors= []
685691

686-
foritemindata:
692+
foridx,iteminenumerate(data):
693+
if (
694+
hasattr(self,'instance')
695+
andself.instance
696+
andlen(self.instance)>idx
697+
):
698+
self.child.instance=self.instance[idx]
687699
try:
688700
validated=self.child.run_validation(item)
689701
exceptValidationErrorasexc:

‎tests/test_serializer.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
importpickle
33
importre
44
importsys
5+
importunittest
56
fromcollectionsimportChainMap
67
fromcollections.abcimportMapping
78

@@ -783,3 +784,63 @@ def test_nested_key(self):
783784
ret= {'a':1}
784785
self.s.set_value(ret, ['x','y'],2)
785786
assertret== {'a':1,'x': {'y':2}}
787+
788+
789+
classMyClass(models.Model):
790+
name=models.CharField(max_length=100)
791+
value=models.CharField(max_length=100,blank=True)
792+
793+
app_label="test"
794+
795+
@property
796+
defis_valid(self):
797+
returnself.name=='valid'
798+
799+
800+
classMyClassSerializer(serializers.ModelSerializer):
801+
classMeta:
802+
model=MyClass
803+
fields= ('id','name','value')
804+
805+
defvalidate_value(self,value):
806+
ifvalueandnotself.instance.is_valid:
807+
raiseserializers.ValidationError(
808+
'Status cannot be set for invalid instance')
809+
returnvalue
810+
811+
812+
classTestMultipleObjectsValidation(unittest.TestCase):
813+
defsetUp(self):
814+
self.objs= [
815+
MyClass(name='valid'),
816+
MyClass(name='invalid'),
817+
MyClass(name='other'),
818+
]
819+
820+
deftest_multiple_objects_are_validated_separately(self):
821+
822+
serializer=MyClassSerializer(
823+
data=[{'value':'set','id':instance.id}forinstancein
824+
self.objs],
825+
instance=self.objs,
826+
many=True,
827+
partial=True,
828+
)
829+
830+
assertnotserializer.is_valid()
831+
assertserializer.errors== [
832+
{},
833+
{'value': ['Status cannot be set for invalid instance']},
834+
{'value': ['Status cannot be set for invalid instance']}
835+
]
836+
837+
deftest_exception_raised_when_data_and_instance_length_different(self):
838+
839+
withself.assertRaises(AssertionError):
840+
MyClassSerializer(
841+
data=[{'value':'set','id':instance.id}forinstancein
842+
self.objs],
843+
instance=self.objs[:-1],
844+
many=True,
845+
partial=True,
846+
)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp