1

when ever i send the data from postman api it says

IntegrityError at /api/movies/2/rate_movie/ UNIQUE constraint failed: api_rating.user_id, api_rating.movie_id

i tried

Rating.objects.update_or_create(user=user, movie=movie, defaults={stars:stars})

this too in views but after trying this data is accepted but it donot update the data

here is my views.py

from django.shortcuts import render
from rest_framework import viewsets, status
from rest_framework.response import Response
from .models import Movie, Rating
from .serializers import MovieSerializer, RatingSerializer 
from rest_framework.decorators import action
from django.contrib.auth.models import User
# Create your views here.
class MovieViewSet(viewsets.ModelViewSet):
    queryset = Movie.objects.all()
    serializer_class = MovieSerializer

    @action(detail=True, methods=['POST'])
    def rate_movie(self, request, pk=None):
        if 'stars' in request.data:
            movie = Movie.objects.get(id=pk)
            stars = request.data['stars']
            #user = request.user
            user = User.objects.get(id=1)
            print('user',user)
            print('movie title', movie.title)
            try:
                rating = Rating.object.get(user=user.id, movie=movie.id)
                rating.stars = stars
                rating.save()
            except:
                Rating.objects.update_or_create(user=user, movie=movie, stars=stars)


            response = {'message':'its working'}
            return Response(response, status=status.HTTP_200_OK)
        else:
            response = {'message':'you need to provide stars'}
            return Response(response, status=status.HTTP_400_BAD_REQUEST)


class RatingViewSet(viewsets.ModelViewSet):
    queryset = Rating.objects.all()
    serializer_class = RatingSerializer
    

and here is my models.py

from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MaxValueValidator, MinValueValidator
# Create your models here.

class Movie(models.Model):
    title = models.CharField(max_length=32)
    description = models.TextField(max_length=360)

class Rating(models.Model):
    movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    stars = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(5)], null=True)
    
    class Meta:
        unique_together = (('user','movie'),)
        index_together = (('user','movie',),)
2
  • This error is occurring due to the repetition of duplicate data related to movie and user. Can you post your model as well to reach a solution? Commented Aug 2, 2020 at 6:03
  • @ArvindKumar i have posted my model
    – Noob_Gamer
    Commented Aug 2, 2020 at 7:41

1 Answer 1

0

I think the problem is occurring where you are trying to save the rating. I've updated that part - first filter to get the correct entry, then get the rating, and then update the entry with the stars.

@action(detail=True, methods=['POST'])
def rate_movie(self, request, pk=None):
    if 'stars' in request.data:
        movie = Movie.objects.get(id=pk)
        stars = request.data['stars']
        #user = request.user
        user = User.objects.get(id=1)
        print('user',user)
        print('movie title', movie.title)
        rating = Rating.object.filter(user=user.id, movie=movie.id)
        rating.stars = stars
        rating.update(stars = stars)
       

Not the answer you're looking for? Browse other questions tagged or ask your own question.