I am trying to make a simple API that takes in an audio file, reverses it, and stores them both. It can accept the file, and overwriting the perform_create()
method I reversed it. But now I can’t find a way to update the record with the reversed file. This is my viewset
class SoundViewSet(viewsets.ModelViewSet):
queryset = Sound.objects.all().order_by('id')
serializer_class = SoundSerializer
def perform_create(self, serializer):
serializer.save()
sound = Sound.objects.last()
root = settings.MEDIA_ROOT.replace('/media','')
root = root + sound.audio.url
p = Path(root)
clip = AudioSegment.from_wav(p)
rev = clip.reverse()
sound.reversedaudio = rev
serializer = SoundSerializer(data=sound)
serializer.is_valid()
Sound.objects.filter(id=sound.id).update(reversedaudio=serializer.data.get('reversedaudio'))
It is returning NULL values. I have to pass it through the serializer first because the audio file has to be parsed for JSON. I just can’t seem to find a simple way to call a serializer update() like the perform_create(). I tried Sound.objects.filter(id=sound.id).update(reversedaudio=sound.reversedaudio)
but its a viewset update, file gets uploaded un-parsed and is basically then garbage. Here’s my serializer class:
class SoundSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Sound
fields = ('name', 'audio', 'reversedaudio')
Can someone point out what I’m missing?
P.s. I am using DRF for the first time and working under a deadline so I would prefer not having to define everything on my own and only working around the model classes if possible.