Ejemplo n.º 1
0
    def post(self, request, format=None):
        print "movie post method"
       
        movie_data = MoviesData()
        movie_data.name = request.POST.get('name')
        movie_data.ssdb_score = int(request.POST.get('ssdb_score'))
        movie_data.popularity = int(request.POST.get('popularity'))
        movie_data.director = request.POST.get('director')
        movie_data.description = request.POST.get('description')
        movie_data.stars = request.POST.get('stars')
        movie_data.save()


        for genre_id in request.POST.getlist('genre[]'):
            movie_data.genre.add(int(genre_id))    
       
        dict_data = model_to_dict(movie_data)
        print "previous data",request.data
        print "dict data",dict_data
        # import pdb
        # pdb.set_trace()
        serializer = MoviesDataSerializer(data=dict_data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
    def put(self, request, pk, format=None):         
        movie = self.get_object(pk)
        genre_list = []
        movie_data = {}
        data = request.data
        movie_data = dict(data.iterlists())       
        genre_list = [int(genre_id) for genre_id in movie_data.get('genre[]')]
                
        movie_data['genre'] = genre_list
        movie_data['name'] = request.data['name']
        movie_data['director'] = request.data['director']
        movie_data['description'] = request.data['description']
        movie_data['stars'] = request.data['stars']
        movie_data['ssdb_score'] = int(request.data['ssdb_score'])
        movie_data['popularity'] = int(request.data['popularity'])

        movie_data['create_date'] = None
        
        serializer = MoviesDataSerializer(movie, data=movie_data)        
        if serializer.is_valid():            
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)