Mar-27-2024, 04:56 AM
Hi All,
I'm currently learning the basic methods of DjangoRESTFramework. It's the first time I've learned how to make and use an API. I'm just playing around, pretending that it's an API for scores for staff members in a company.
I'm having an error when I submit a POST request to the server. I can't for the life of me understand why, although I assume it's some small syntactical error or just missing a line that I can't debug myself because it's all new to me at the moment.
I've created a token for a superuser, and I'm posting this token to the server from Insomnia, along with "staff_id", 3, and "score", "5" as the query parameters.
It comes up with a 500 error, and says:
models.py
views.py
serializers.py
If I remove the extra kwargs and just try posting a staff_id and score of whatever numbers I choose, it just comes up with a 400 bad request. Is it that I need some POST function in the views.py?
Any help would be appreciated. Hopefully I've formatted the BBCode properly...
Thanks
I'm currently learning the basic methods of DjangoRESTFramework. It's the first time I've learned how to make and use an API. I'm just playing around, pretending that it's an API for scores for staff members in a company.
I'm having an error when I submit a POST request to the server. I can't for the life of me understand why, although I assume it's some small syntactical error or just missing a line that I can't debug myself because it's all new to me at the moment.
I've created a token for a superuser, and I'm posting this token to the server from Insomnia, along with "staff_id", 3, and "score", "5" as the query parameters.
It comes up with a 500 error, and says:
Error:...
Exception Type: TypeError
Exception Value:
Field.__init__() got an unexpected keyword argument 'min_value'
...
This is the relevant code:models.py
1 2 3 4 5 6 7 |
from django.db import models from django.contrib.auth.models import User class Score(models.Model): user = models.ForeignKey(User, on_delete = models.CASCADE) staff_id = models.SmallIntegerField score = models.SmallIntegerField |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from rest_framework import generics from rest_framework.permissions import IsAuthenticated from .models import Score from .serializers import ScoreSerializer class ScoreView(generics.ListCreateAPIView): queryset = Score.objects. all () serializer_class = ScoreSerializer def get_permissions( self ): if ( self .request.method = = 'GET' ): return [] return [IsAuthenticated()] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from rest_framework import serializers from .models import Score from rest_framework.validators import UniqueTogetherValidator from django.contrib.auth.models import User class ScoreSerializer (serializers.ModelSerializer): user = serializers.PrimaryKeyRelatedField( queryset = User.objects. all (), default = serializers.CurrentUserDefault() ) class Meta(): model = Score fields = [ 'user' , 'staff_id' , 'score' ] validators = [ UniqueTogetherValidator( queryset = Score.objects. all (), fields = [ 'user' , 'staff_id' ] ) ] extra_kwargs = { 'score' : { 'min_value' : 0 , 'max_value' : 10 }, } |
Any help would be appreciated. Hopefully I've formatted the BBCode properly...
Thanks