On Tue, Nov 13, 2012 at 6:37 AM, Josue Balandrano <xird...@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-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to