Last active
April 14, 2020 23:57
-
-
Save bismarck4lves/a3f5185fdd34d1115937106b2f5d4b40 to your computer and use it in GitHub Desktop.
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
class AgendasSerializer(serializers.ModelSerializer): | |
horario = serializers.SlugRelatedField( | |
many=True, | |
read_only=True, | |
slug_field='hora', | |
) | |
def to_representation(self, instance): | |
data = super(AgendasSerializer, self).to_representation(instance) | |
def valida_datas(dia, hora, agenda_id): | |
#validadores ... | |
return False # exemplo | |
data['horario'] = list(filter(lambda hora: valida_datas(data['dia'], hora, data['id']), data['horario'])) | |
if not data['horario']: | |
return None | |
return data | |
class Meta: | |
model = Agendas | |
fields = ['id', 'dia', 'horario'] | |
Nessa Logica meu JSON está terminando assim: | |
[ | |
null, # esse null é pq no representation ele retornou None | |
{ | |
"id": 4, | |
"dia": "02-04-2020", | |
"horario": [ | |
"15:30:00", | |
"01:17:41" | |
] | |
} | |
] | |
E eu quero que retorn assim: | |
[ | |
{ | |
"id": 4, | |
"dia": "02-04-2020", | |
"horario": [ | |
"15:30:00", | |
"01:17:41" | |
] | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment