SOLVED: (I think)
Though I do not understand the how/why. From an example on
StackOverflow I tried changing the post method to this:
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
# <process form cleaned data>
paper = get_object_or_404(Paper,pk=kwargs['pk'])
reviewer =
Reviewer.objects.filter(user=self.request.user).filter(prj_name=paper.project)
f = form.save(commit=False)
f.reviewer = reviewer[0]
f.paper = paper
f.stage = paper.current_stage
f.save()
return HttpResponseRedirect('#')
return render(request, self.template_name, {'form': form})
and it works. Now to study more about saving the form to a new object
and then adding the additional data before saving with a commit. Very
confusing ...
On Sun, Nov 24, 2013 at 11:46 AM, Timothy W. Cook <tim@mlhim.org> wrote:
> I have an issue with save data from a form.
>
> The actual form has one required and two optional fields.
> The model has three additional fields that I want to assign based on
> the context, two of those are ForeignKey related.
>
> The post method in the view:
> def post(self, request, *args, **kwargs):
> form = self.form_class(request.POST)
>
> if form.is_valid():
> # <process form cleaned data>
> paper = get_object_or_404(Paper,pk=kwargs['pk'])
> reviewer =
> Reviewer.objects.filter(user=self.request.user).filter(prj_name=paper.project)
> form.cleaned_data['reviewer'] = reviewer[0]
> form.cleaned_data['paper'] = paper
> form.cleaned_data['stage'] = paper.current_stage
> print(form.cleaned_data)
> form.save()
> return HttpResponseRedirect('#')
> return render(request, self.template_name, {'form': form})
>
> The form:
> class ReviewTitleCreateForm(ModelForm):
> class Meta:
> model = Review
> fields = ['include','exclusion_choice','exclusion_text',]
>
>
> The model:
> class Review(models.Model):
> """
> A review at a specific stage of one paper.
> """
> INCLUDE_CHOICES = [(True,'Include'),(False,'Exclude')]
> STAGES = [('Selection by Title','Selection by Title'),('Selection
> by Abstract','Selection by Abstract'),
> ('Selection by Full Text','Selection by Full Text')]
>
> reviewer = models.ForeignKey(Reviewer, verbose_name=_('Reviewer'),
> related_name="%(app_label)s_%(class)s_related", null=False,
> blank=False, help_text=_("The reviewer of this paper, at this
> stage."))
> paper = models.ForeignKey(Paper, verbose_name=_('Paper'),
> related_name="%(app_label)s_%(class)s_related", null=False,
> blank=False, help_text=_("The reviewed paper."))
> review_stage = models.CharField("Review Stage",max_length=30,
> choices=STAGES, help_text="The stage for review of this paper.")
> include = models.NullBooleanField("Choice",
> choices=INCLUDE_CHOICES, default=True, null=False, blank=False,
> help_text="Select Include or Exclude for this stage of review.")
> exclusion_choice = models.ForeignKey(Exclusion, null=True,
> blank=True, related_name="%(app_label)s_%(class)s_related")
> exclusion_text = models.TextField(null=True, blank=True)
>
>
> When I output the cleaned_data from the print at the bottom of the
> post method (see above), I get this:
> {'reviewer': <Reviewer: Tim Cook>, 'include': True, 'paper': <Paper:
> Assessing the quality of clinical data in a computer-based record for
> calculating the pneumonia severity index.>, 'exclusion_text': '',
> 'stage': 'Selection by Title', 'exclusion_choice': None}
>
>
> Which looks correct, to me. However I am getting an error as if there
> is no Reviewer assigned:
> IntegrityError at /papers/review_title/3
> null value in column "reviewer_id" violates not-null constraint
> Request Method:POST
>
> Any ideas?
>
> Thanks,
> Tim
>
>
>
>
>
>
>
>
>
> --
> MLHIM VIP Signup: http://goo.gl/22B0U
> ============================================
> Timothy Cook, MSc +55 21 94711995
> MLHIM http://www.mlhim.org
> Like Us on FB: https://www.facebook.com/mlhim2
> Circle us on G+: http://goo.gl/44EV5
> Google Scholar: http://goo.gl/MMZ1o
> LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook
--
MLHIM VIP Signup: http://goo.gl/22B0U
============================================
Timothy Cook, MSc +55 21 94711995
MLHIM http://www.mlhim.org
Like Us on FB: https://www.facebook.com/mlhim2
Circle us on G+: http://goo.gl/44EV5
Google Scholar: http://goo.gl/MMZ1o
LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B%3DOU3XV7c0fjvzkey3Pzz49GvWKUJPHB5MKpG8hSsuPp7TJ%3DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment