@@ -16,6 +16,10 @@ class Meta:
1616 model = PresentationSpeaker
1717 fields = ("id" , "biography_ko" , "biography_en" , "image" , "user" )
1818
19+ def to_internal_value (self , data : dict ) -> dict :
20+ """Override to_internal_value to ensure that the 'id' field is included in the validated data."""
21+ return super ().to_internal_value (data ) | {"id" : data .get ("id" )}
22+
1923
2024class PresentationSpeakerPortalData (typing .TypedDict ):
2125 id : str | uuid .UUID
@@ -70,22 +74,19 @@ def to_representation(self, instance):
7074
7175 return result
7276
73- def validate (self , attrs : dict ) -> dict :
74- attrs = super ().validate (attrs )
75-
76- speakers = typing .cast (list [PresentationSpeakerPortalData ], attrs ["speakers" ])
77- if not isinstance (speakers , list ):
77+ def validate_speakers (self , value : list [PresentationSpeakerPortalData ]) -> list [PresentationSpeakerPortalData ]:
78+ if not isinstance (value , list ):
7879 raise serializers .ValidationError ("Speakers must be a list." )
7980
80- for speaker_data in speakers :
81- if not (speaker_instance := self .get_speaker_instance (speaker_data ["id" ])):
81+ for speaker_data in value :
82+ if not isinstance (speaker_data , dict ):
83+ raise serializers .ValidationError ("Each speaker must be a dictionary." )
84+
85+ if "id" not in speaker_data or not speaker_data ["id" ]:
86+ raise serializers .ValidationError ("Each speaker must have a valid ID." )
87+
88+ if not self .get_speaker_instance (speaker_data ["id" ]):
8289 err_msg = f"Speaker with ID { speaker_data ['id' ]} not found or does not belong to this presentation."
8390 raise serializers .ValidationError (err_msg )
8491
85- PresentationSpeakerPortalSerializer (
86- instance = speaker_instance ,
87- data = speaker_data ,
88- partial = True ,
89- ).is_valid (raise_exception = True )
90-
91- return attrs
92+ return value
0 commit comments