Re: ModelForm displays invalid cohice error.

69 views
Skip to first unread message

Lachlan Musicman

unread,
Nov 12, 2012, 2:40:38 PM11/12/12
to django...@googlegroups.com
On Tue, Nov 13, 2012 at 6:37 AM, Josue Balandrano <xir...@gmail.com> wrote:
> Hi, I am trying to create a very basic ratings and comments app. I have this
> model:
>
> class Ratings(models.Model):
> user = models.ForeignKey(UserProfile)
> product = models.ForeignKey(Product)
> rate = models.PositiveIntegerField(default=0, null=False,
> choices=((1,'1'),(2,'2'),(3,'3'),(4,'4'),(5,'5')))

Can a choices kwarg be used with a PositiveIntegerField? Is the
problem solved by changing this to a CharField?



> comment = models.TextField(default='', blank=True)
> date_added = models.DateField(default=datetime.now)
> objects = RatingsManager()
>
> def __unicode__(self):
> return self.rating
>
> class Meta:
> unique_together = ("user", "restaurant")
>
> class RatingsManager(models.Manager):
> def get_average(self, **kwargs):
> prod = kwargs['prod']
> return self.filter(product = prod).aggregate(Avg('rate'))
>
> And I have this modelform Class:
>
> class RatingsForm(forms.ModelForm):
> comment =
> forms.CharField(widget=forms.Textarea(attrs={'name':'comentario',
> 'label':'', 'rows':'5', 'cols':'50'}))
> class Meta:
> model = Ratings
> widgets = {
> 'user' : forms.HiddenInput(),
> 'product' : forms.HiddenInput(),
> 'rate' : forms.RadioSelect(),
> }
> exclude = ('date_added', )
>
> First, this is how I initiate the form and pass it to the context variable.
> This form will only ve bisivle in a detail view of the product.
>
> rating_form = RatingsForm(initial={'product':prod.id,
> 'user':self.request.user.id})
> context['rating_form'] = rating_form
>
> And this is what I gets executed once the view recieves a post request.
>
> product_id = request.POST.get('product')
> user_id = request.user.id
> try:
> instance = Ratings.objects.get(user = int(user_id), product =
> int(product_id)) #IF USER SUBMITED RATING FOR THIS PRODUCT UPDATE EXISTING
> RECORD INSTEAD OF CREATING A NEW ONE
> except Ratings.DoesNotExist:
> instance = None
> if instance is None:
> formset = RatingsForm(request.POST)
> formset.restaurant = int(restaurant_id)
> formset.user = int(user_id)
> if formset.is_valid():
> formset.save()
>
> else:
> formset = RatingsForm(request.POST, instance=instance)
> if formset.is_valid():
> formset.save()
>
> context.update({ 'rating_form':formset,})
> return self.render_to_response(context)
>
> So when ever I submit the form I get this error:
>
> Select a valid choice. That choice is not one of the available choices.
>
> But I can't figure out what does it means? Because the only choice field I
> have is the "rate" field and it could never be anything else than an int
> between 1 - 5.
>
> Any ideas how to fix this?
>
> Thank you.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/u6heU1dlyDsJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.



--
...we look at the present day through a rear-view mirror. This is
something Marshall McLuhan said back in the Sixties, when the world
was in the grip of authentic-seeming future narratives. He said, “We
look at the present through a rear-view mirror. We march backwards
into the future.”

http://www.warrenellis.com/?p=14314

Jill Green

unread,
Nov 12, 2012, 2:55:15 PM11/12/12
to django...@googlegroups.com
Doesn't the default need to be one of the choices?

models.PositiveIntegerField(default=1, null=False, choices=((1,'1'),(2,'2'),(3,'3'),(4,'4'),(5,'5')))

Sent from my iPhone

On Nov 12, 2012, at 11:37 AM, Josue Balandrano <xir...@gmail.com> wrote:

Hi, I am trying to create a very basic ratings and comments app. I have this model:

class Ratings(models.Model):
    user = models.ForeignKey(UserProfile)
    product = models.ForeignKey(Product)
    rate = models.PositiveIntegerField(default=0, null=False, choices=((1,'1'),(2,'2'),(3,'3'),(4,'4'),(5,'5')))

Josue Balandrano

unread,
Nov 12, 2012, 3:52:06 PM11/12/12
to django...@googlegroups.com
Judging by the docs it really doesn't matter the kind of field it is. As long as it has de choices tuple list. Giving that this tuple gives you the value it should take and the human readable form. So I am guessing django framework just mapps the labels with the values you give in the choices tuples. So as long as you use Positive Integers in the choice tuple it should be fine.
HAving said this I am going to try to change it into a charfield to see what happens.

Josue Balandrano

unread,
Nov 12, 2012, 3:52:46 PM11/12/12
to django...@googlegroups.com
Iam really not shure. Will try to chane this.

Lachlan Musicman

unread,
Nov 12, 2012, 4:18:38 PM11/12/12
to django...@googlegroups.com
On Tue, Nov 13, 2012 at 8:52 AM, Josue Balandrano <xir...@gmail.com> wrote:
> Judging by the docs it really doesn't matter the kind of field it is. As
> long as it has de choices tuple list. Giving that this tuple gives you the
> value it should take and the human readable form. So I am guessing django
> framework just mapps the labels with the values you give in the choices
> tuples. So as long as you use Positive Integers in the choice tuple it
> should be fine.
> HAving said this I am going to try to change it into a charfield to see what
> happens.

I actually think you will have more luck with Jill's suggestion :)

cheers
L.
> https://groups.google.com/d/msg/django-users/-/pjjgxDzWwgIJ.
Reply all
Reply to author
Forward
0 new messages