title: Django - user rate profile
body: <p>I am a newbie to Django and this question might be an easy one, but it has already been bothering me for a while. </p>
  
  <pre><code>&lt;form method="post" action="/vote/" class="vote_form"&gt;
     &lt;input type="hidden" id="id_link" name="user_profile" class="hidden_id" value="5" /&gt;
     &lt;input type="hidden" id="id_voter" name="voter" class="hidden_id" value="3" /&gt;
      New Score: &lt;input name="score" class="" value="7" /&gt;
      &lt;button type="submit"&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>urls.py</p>
  
  <pre><code>url(r'^vote/$', auth(VoteFormView.as_view()), name="vote"),
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>class VoteFormView(FormView):
      form_class = VoteForm
  
      def create_response(self, vdict=dict(), valid_form=True):
          response = HttpResponse(json.dumps(vdict))
          response.status = 200 if valid_form else 500
          return response
  
      def form_valid(self, form):
          profile = get_object_or_404(UserProfile, pk=form.data['user_profile'])
          user = self.request.user
          score = form.cleaned_data['score']
  
          prev_votes = Vote.objects.filter(voter=user, profile=profile)
          has_voted = (len(prev_votes) &gt; 0)
  
          ret = {"success": 1, "profile": profile, "voter: ": user}
          if not has_voted:
              # add vote
              v = Vote.objects.create(voter=user, profile=profile, score=score)
              ret["voteobj"] = v.id
              # else response already ranked  
          return self.create_response(ret, True)
  
  
      def form_invalid(self, form):
          ...do something when form invalid...
  </code></pre>
  
  <p>forms.py</p>
  
  <pre><code>class VoteForm(forms.ModelForm):
     class Meta:
       model = Vote
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>class Vote(models.Model):
      voter = models.ForeignKey(User)
      profile = models.ForeignKey(UserProfile)
      score = models.FloatField(default=10.0)
  
      def __unicode__(self):
          return "%s voted %s" % (self.voter.username, self.profile)
  
  class UserProfile(models.Model):
      user = models.OneToOneField(User, unique=True)
      # Extra attributes
  </code></pre>
  
  <p>As you can see, I have a simple form with 2 fields: a user_profile (PK of a user profile) and a score. After the form is submitted, it has this error: <code>"form_errors": {"profile": ["This field is required."]}</code> . So it always goes to to <code>form_invalid</code>. 
  Where am I supposed to get profile object if it is not form_valid? 
  I tried to redefine VoteForm as below, which does not work either.</p>
  
  <pre><code>class VoteForm(forms.ModelForm):
      def clean_profile(self):
          profile_pk = self.cleaned_data['profile']
          profile = None
          try:
              profile = UserProfile.objects.get(pk=profile_pk)
          except UserProfile.DoesNotExist:
              raise forms.ValidationError("User profile doesn't exist.")
          return profile
  
      def save(self, *args, **kwargs):
          vote = super(VoteForm, self).save(*args, **kwargs)
          return vote
  
      class Meta:
          model = Vote
  </code></pre>
  
  <p>Any ideas?</p>