示例#1
0
def song_list(request):
    if request.method == 'GET':
        songs = song.objects.all()
        Id = request.query_params.get('Id', None)
        if Id is not None:
            songs = songs.filter(title__icontains=Id)

        songs_serializer = songSerializer(songs, many=True)
        return JsonResponse(songs_serializer.data, safe=False)
        #'safe=False' for objects serialization

    elif request.method == 'POST':

        song_data = JSONParser().parse(request)
        song_serializer = songSerializer(data=song_data)
        if song_serializer.is_valid():
            song_serializer.save()
            return JsonResponse(song_serializer.data,
                                status=status.HTTP_201_CREATED)
        return JsonResponse(song_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
示例#2
0
def song_d(request, pk):
    try:
        song = song.objects.get(pk=pk)
    except song.DoesNotExist:
        return JsonResponse({'message': 'The song does not exist'},
                            status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        song_serializer = songSerializer(song)
        return JsonResponse(song_serializer.data)

    elif request.method == 'PUT':
        song_data = JSONParser().parse(request)
        song_serializer = songSerializer(song, data=song_data)
        if song_serializer.is_valid():
            song_serializer.save()
            return JsonResponse(song_serializer.data)
        return JsonResponse(song_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        song.delete()
        return JsonResponse({'message': 'song was deleted successfully!'},
                            status=status.HTTP_204_NO_CONTENT)
示例#3
0
def song_published(request):
    songs = song.objects.filter(published=True)

    if request.method == 'GET':
        song_serializer = songSerializer(tutorials, many=True)
        return JsonResponse(song_serializer.data, safe=False)