0

I am trying to convert the data from a choicefield into an INT, but gets the error:

"int() argument must be a string, a bytes-like object or a number, not 'TypedChoiceField'"

However, I don't understand why the coerce=int, doesn't convert the data from the choicefield into an int.

My forms.py class

class ReviewForm(forms.Form):
readability_rating = forms.TypedChoiceField(
    choices=[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)], coerce=int)

My views.py function

def readerpage(request, content_id):
   form = ReviewForm(request.POST)
   if form.is_valid():
        review.readability_rating = forms.TypedChoiceField(
                        choices=[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)], coerce=int)
        review.save()
        form = ReviewForm()
        return redirect('home') 

My model

class Review(models.Model):
   readability_rating = models.IntegerField(null=True)

Thanks for reading this

Dybton
  • 75
  • 8

1 Answers1

0

This assignment is a mistake, this is not how forms work:

if form.is_valid():
    review.readability_rating = forms.TypedChoiceField(
                        choices=[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)], coerce=int)

Please, go through forms tutorial.

And also check this answer: How to get value from form field in django framework?)

If you seek good understanding of how Django works, I would also recommend a Book: Apress Pro Django.

I hope it'll help.

Ilya
  • 1,349
  • 11
  • 16