def create(self, validated_data): """Four things must be done, excluding saving the Team object: 1. Create the short_reference based on 'reference', 2. Create emoji/code name if not stated, 3. Create the speakers. 4. Add institution conflict""" if len(validated_data.get('short_reference', "")) == 0: validated_data['short_reference'] = validated_data[ 'reference'][:34] speakers_data = validated_data.pop('speakers', []) break_categories = validated_data.pop('break_categories', []) emoji, code_name = pick_unused_emoji() if 'emoji' not in validated_data or validated_data.get( 'emoji') is None: validated_data['emoji'] = emoji if 'code_name' not in validated_data or validated_data.get( 'code_name') is None: validated_data['code_name'] = code_name if validated_data['emoji'] == '': validated_data[ 'emoji'] = None # Must convert to null to avoid uniqueness errors team = super().create(validated_data) # Add general break categories team.break_categories.set( list( BreakCategory.objects.filter( tournament=team.tournament, is_general=True, ).exclude(pk__in=[bc.pk for bc in break_categories])) + break_categories) # The data is passed to the sub-serializer so that it handles categories if len(speakers_data) > 0: speakers = SpeakerSerializer(many=True, context=self.context) speakers._validated_data = speakers_data # Data was already validated speakers.save(team=team) if team.institution is not None: team.teaminstitutionconflict_set.get_or_create( institution=team.institution) return team
def create(self, validated_data): """Four things must be done, excluding saving the Team object: 1. Create the short_reference based on 'reference', 2. Create emoji/code name if not stated, 3. Create the speakers. 4. Add institution conflict""" if len(validated_data['short_reference']) == 0: validated_data['short_reference'] = validated_data[ 'reference'][:34] speakers_data = validated_data.pop('speakers') break_categories = validated_data.pop('break_categories') emoji, code_name = pick_unused_emoji() if 'emoji' not in validated_data: validated_data['emoji'] = emoji if 'code_name' not in validated_data: validated_data['code_name'] = code_name team = super().create(validated_data) team.break_categories.set( list( BreakCategory.objects.filter(tournament=team.tournament, is_general=True)) + break_categories, ) # The data is passed to the sub-serializer so that it handles categories speakers = SpeakerSerializer(many=True, context={'tournament': team.tournament}) speakers._validated_data = speakers_data # Data was already validated speakers.save(team=team) if team.institution is not None: team.teaminstitutionconflict_set.create( institution=team.institution) return team