4

I am developing a rest API that gets requests composed of multiple objects and saves them to the database. Then, another array of objects is returned as the response. All objects are of only one model.

class Ratings(models.Model):
    id = models.AutoField(primary_key = True)
    userId = models.IntegerField()
    movieId = models.IntegerField()
    rating = models.PositiveIntegerField(validators=[MinValueValidator(1), MaxValueValidator(5)])
    timestamp = models.DateTimeField(auto_now = True)

class RatingsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Ratings
        fields = ('userId','movieId','rating')

class RecommendationGenerator(generics.ListCreateAPIView):
    queryset = Ratings.objects.all()
    serializer_class= RatingsSerializer
    def post(self, request, format='json'):
        serializer= RatingsSerializer(data = request.data, many = True)
        if serializer.is_valid():
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)

When I test it in Postman with the JSON:

[
    {
        "userId": 13,
        "movieId": 1765,
        "rating": 5
    },
    {
        "userId": 13,
        "movieId": 1733,
        "rating": 3
    },
    {
        "userId": 13,
        "movieId": 1713,
        "rating": 2
    },
    {
        "userId": 13,
        "movieId": 963,
        "rating": 2
    }
]

The result is []. But for

{
    "userId": 13,
    "movieId": 1765,
    "rating": 5
}

The result is

{
    "userId": 13,
    "movieId": 1765,
    "rating": 5
}

How do I deserialize this data? What am I doing wrong here?

2
  • you tried with queryset = Ratings.objects.all() ?? as i see commented all() Commented Feb 27, 2018 at 10:08
  • Yeah I did. I had to edit it out because the localhost was displaying all the objects in the database, which was lagging the browser. Commented Feb 27, 2018 at 10:14

2 Answers 2

2

views should be like

class RecommendationGenerator(generics.ListCreateAPIView):
    queryset = Ratings.objects.all()
    serializer_class= RatingsSerializer

    def post(self, request, format='json'):
        serializer= RatingsSerializer(data = request.data, many = True)
        if serializer.is_valid():
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)

Try This

Sign up to request clarification or add additional context in comments.

4 Comments

is this code saving any items? I don't see any call to serializer.save() after validation
Not yet. Because I wanted to validate whether the deserialization works, and it doesn't seem to yet. :(. The plan is to save them all, then apply a recommender system algorithm to them and send back an array of Rating objects
@Shihabudheen K M It was already like this though. Just corrected the code. Thanks for pointing it out.
It would be better if you provide the reasons caused the error/bug
2

The code works fine. The problem was that the request sent was in the form of Text instead of 'application/json'. Thanks for the help anyways.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.