The models.py
IntegerField is set-up:
class DMCAModel(models.Model):
tick = models.CharField(max_length=20)
percent_calc = models.FloatField()
ratio = models.IntegerField(blank=True, null=True, default=None) #The problem is here
created_at = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(User, related_name='dmca',
on_delete=models.CASCADE, null=True)
serilaizers.py
class DMCASerializer(serializers.ModelSerializer):
class Meta:
model = DMCAModel
fields = '__all__'
# fields = ['id', 'tick', 'percent_calc', 'ratio']
# I have tried removing fields, if I remove 'ratio' then even if I send an input it is ignored.
api.py
class DmcaViewSet(viewsets.ModelViewSet):
# queryset = DMCAModel.objects.all()
permission_classes = [
permissions.IsAuthenticated
]
serializer_class = DMCASerializer
def get_queryset(self):
return self.request.user.dmca.all()
def perform_create(self, serializer):
# print(self.request.user)
serializer.save(owner=self.request.user)
When I to make the following POST
request from postman:
Authorization: Token xxxxxx
Body JSON:
{
"tick":"XYZ",
"percent_calc":"3",
"ratio":""
}
I get the error:
{ "ratio": [ "A valid integer is required." ] }
I think I have set the properties of the Models
field correctly if I understand them correctly: null=True
database will set empty to NULL
, blank=True
form input will be accepted, default=None
incase nothing is entered. I have tried to use the properties one by one and see what happens. But can't work out the error.