BooleanField not updating: Django












1















I am learning Django by building an example app in which students can choose to participate in study sections. The participate action is a BooleanField that I would like the students to be able to check or uncheck and update. It is unchecked by default and I am able to check it and save the form. But when I go to update the form the box is unchecked. How can I set up the form, model, and view so that the participate field can be saved and updated?



models.py



class StudentAnswer(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name='study_answers')
study = models.ForeignKey(Study, on_delete=models.CASCADE, related_name='study_participate', null=True)
participate = models.BooleanField('Participate?', default=False)


forms.py



class ViewStudyForm(forms.ModelForm):

class Meta:
model = StudentAnswer
fields = ('participate', )


views.py



@login_required
@student_required
def participate_study(request, pk):
study = get_object_or_404(Study, pk=pk)
student = request.user.student

total_details = study.details.count()
details = student.get_details(study)

if request.method == 'POST':
form = ViewStudyForm(data=request.POST)
if form.is_valid():
with transaction.atomic():
student_answer = form.save(commit=False)
student_answer.student = student
student_answer.save()
messages.success(request, 'Congratulations! You signed up to participate in the study %s!' % (study.name))
return redirect('students:study_list')
else:
form = ViewStudyForm()
progress=100
return render(request, 'classroom/students/past_study_form.html', {
'study': study,
'details': details,
'form': form,
'progress': progress
})









share|improve this question


















  • 1





    try smth like this: ...else: participate = StudentAnswer.objects.get(id=pk).values('participate'); form = ViewStudyForm(initial={'participate': participate})

    – Chiefir
    Jan 2 at 14:40


















1















I am learning Django by building an example app in which students can choose to participate in study sections. The participate action is a BooleanField that I would like the students to be able to check or uncheck and update. It is unchecked by default and I am able to check it and save the form. But when I go to update the form the box is unchecked. How can I set up the form, model, and view so that the participate field can be saved and updated?



models.py



class StudentAnswer(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name='study_answers')
study = models.ForeignKey(Study, on_delete=models.CASCADE, related_name='study_participate', null=True)
participate = models.BooleanField('Participate?', default=False)


forms.py



class ViewStudyForm(forms.ModelForm):

class Meta:
model = StudentAnswer
fields = ('participate', )


views.py



@login_required
@student_required
def participate_study(request, pk):
study = get_object_or_404(Study, pk=pk)
student = request.user.student

total_details = study.details.count()
details = student.get_details(study)

if request.method == 'POST':
form = ViewStudyForm(data=request.POST)
if form.is_valid():
with transaction.atomic():
student_answer = form.save(commit=False)
student_answer.student = student
student_answer.save()
messages.success(request, 'Congratulations! You signed up to participate in the study %s!' % (study.name))
return redirect('students:study_list')
else:
form = ViewStudyForm()
progress=100
return render(request, 'classroom/students/past_study_form.html', {
'study': study,
'details': details,
'form': form,
'progress': progress
})









share|improve this question


















  • 1





    try smth like this: ...else: participate = StudentAnswer.objects.get(id=pk).values('participate'); form = ViewStudyForm(initial={'participate': participate})

    – Chiefir
    Jan 2 at 14:40
















1












1








1








I am learning Django by building an example app in which students can choose to participate in study sections. The participate action is a BooleanField that I would like the students to be able to check or uncheck and update. It is unchecked by default and I am able to check it and save the form. But when I go to update the form the box is unchecked. How can I set up the form, model, and view so that the participate field can be saved and updated?



models.py



class StudentAnswer(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name='study_answers')
study = models.ForeignKey(Study, on_delete=models.CASCADE, related_name='study_participate', null=True)
participate = models.BooleanField('Participate?', default=False)


forms.py



class ViewStudyForm(forms.ModelForm):

class Meta:
model = StudentAnswer
fields = ('participate', )


views.py



@login_required
@student_required
def participate_study(request, pk):
study = get_object_or_404(Study, pk=pk)
student = request.user.student

total_details = study.details.count()
details = student.get_details(study)

if request.method == 'POST':
form = ViewStudyForm(data=request.POST)
if form.is_valid():
with transaction.atomic():
student_answer = form.save(commit=False)
student_answer.student = student
student_answer.save()
messages.success(request, 'Congratulations! You signed up to participate in the study %s!' % (study.name))
return redirect('students:study_list')
else:
form = ViewStudyForm()
progress=100
return render(request, 'classroom/students/past_study_form.html', {
'study': study,
'details': details,
'form': form,
'progress': progress
})









share|improve this question














I am learning Django by building an example app in which students can choose to participate in study sections. The participate action is a BooleanField that I would like the students to be able to check or uncheck and update. It is unchecked by default and I am able to check it and save the form. But when I go to update the form the box is unchecked. How can I set up the form, model, and view so that the participate field can be saved and updated?



models.py



class StudentAnswer(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name='study_answers')
study = models.ForeignKey(Study, on_delete=models.CASCADE, related_name='study_participate', null=True)
participate = models.BooleanField('Participate?', default=False)


forms.py



class ViewStudyForm(forms.ModelForm):

class Meta:
model = StudentAnswer
fields = ('participate', )


views.py



@login_required
@student_required
def participate_study(request, pk):
study = get_object_or_404(Study, pk=pk)
student = request.user.student

total_details = study.details.count()
details = student.get_details(study)

if request.method == 'POST':
form = ViewStudyForm(data=request.POST)
if form.is_valid():
with transaction.atomic():
student_answer = form.save(commit=False)
student_answer.student = student
student_answer.save()
messages.success(request, 'Congratulations! You signed up to participate in the study %s!' % (study.name))
return redirect('students:study_list')
else:
form = ViewStudyForm()
progress=100
return render(request, 'classroom/students/past_study_form.html', {
'study': study,
'details': details,
'form': form,
'progress': progress
})






python django






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 14:35









AmbroseAmbrose

228




228








  • 1





    try smth like this: ...else: participate = StudentAnswer.objects.get(id=pk).values('participate'); form = ViewStudyForm(initial={'participate': participate})

    – Chiefir
    Jan 2 at 14:40
















  • 1





    try smth like this: ...else: participate = StudentAnswer.objects.get(id=pk).values('participate'); form = ViewStudyForm(initial={'participate': participate})

    – Chiefir
    Jan 2 at 14:40










1




1





try smth like this: ...else: participate = StudentAnswer.objects.get(id=pk).values('participate'); form = ViewStudyForm(initial={'participate': participate})

– Chiefir
Jan 2 at 14:40







try smth like this: ...else: participate = StudentAnswer.objects.get(id=pk).values('participate'); form = ViewStudyForm(initial={'participate': participate})

– Chiefir
Jan 2 at 14:40














1 Answer
1






active

oldest

votes


















1














Try something like this:



....
else:
participate = StudentAnswer.objects.get(student=student).values('participate')
form = ViewStudyForm(initial={'participate': participate})


This should take boolean participate from your StudentAnswer instance and assign it to your form.

More informations is here in Django docs.






share|improve this answer
























  • Hi Chiefir. Thank you for the answer. This almost solved my issue, but initial does not play well with bound forms. My solution based off this was to use ... else: instance = StudentAnswer.objects.filter(student_id=patient.pk, study=study.pk).first(); form = ViewStudyForm(instance=instance)

    – Ambrose
    Jan 3 at 23:15













  • What went wrong with my variant?

    – Chiefir
    Jan 4 at 7:14











  • initial filled the participate field but left the ForeignKey fields for patient and study blank, so I could not save the form. I was going to bypass this behavior by adding the patient and study fields as hidden, but then I read about instance and it seemed like a simpler solution.

    – Ambrose
    Jan 4 at 13:42











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008202%2fbooleanfield-not-updating-django%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Try something like this:



....
else:
participate = StudentAnswer.objects.get(student=student).values('participate')
form = ViewStudyForm(initial={'participate': participate})


This should take boolean participate from your StudentAnswer instance and assign it to your form.

More informations is here in Django docs.






share|improve this answer
























  • Hi Chiefir. Thank you for the answer. This almost solved my issue, but initial does not play well with bound forms. My solution based off this was to use ... else: instance = StudentAnswer.objects.filter(student_id=patient.pk, study=study.pk).first(); form = ViewStudyForm(instance=instance)

    – Ambrose
    Jan 3 at 23:15













  • What went wrong with my variant?

    – Chiefir
    Jan 4 at 7:14











  • initial filled the participate field but left the ForeignKey fields for patient and study blank, so I could not save the form. I was going to bypass this behavior by adding the patient and study fields as hidden, but then I read about instance and it seemed like a simpler solution.

    – Ambrose
    Jan 4 at 13:42
















1














Try something like this:



....
else:
participate = StudentAnswer.objects.get(student=student).values('participate')
form = ViewStudyForm(initial={'participate': participate})


This should take boolean participate from your StudentAnswer instance and assign it to your form.

More informations is here in Django docs.






share|improve this answer
























  • Hi Chiefir. Thank you for the answer. This almost solved my issue, but initial does not play well with bound forms. My solution based off this was to use ... else: instance = StudentAnswer.objects.filter(student_id=patient.pk, study=study.pk).first(); form = ViewStudyForm(instance=instance)

    – Ambrose
    Jan 3 at 23:15













  • What went wrong with my variant?

    – Chiefir
    Jan 4 at 7:14











  • initial filled the participate field but left the ForeignKey fields for patient and study blank, so I could not save the form. I was going to bypass this behavior by adding the patient and study fields as hidden, but then I read about instance and it seemed like a simpler solution.

    – Ambrose
    Jan 4 at 13:42














1












1








1







Try something like this:



....
else:
participate = StudentAnswer.objects.get(student=student).values('participate')
form = ViewStudyForm(initial={'participate': participate})


This should take boolean participate from your StudentAnswer instance and assign it to your form.

More informations is here in Django docs.






share|improve this answer













Try something like this:



....
else:
participate = StudentAnswer.objects.get(student=student).values('participate')
form = ViewStudyForm(initial={'participate': participate})


This should take boolean participate from your StudentAnswer instance and assign it to your form.

More informations is here in Django docs.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 14:47









ChiefirChiefir

1,212315




1,212315













  • Hi Chiefir. Thank you for the answer. This almost solved my issue, but initial does not play well with bound forms. My solution based off this was to use ... else: instance = StudentAnswer.objects.filter(student_id=patient.pk, study=study.pk).first(); form = ViewStudyForm(instance=instance)

    – Ambrose
    Jan 3 at 23:15













  • What went wrong with my variant?

    – Chiefir
    Jan 4 at 7:14











  • initial filled the participate field but left the ForeignKey fields for patient and study blank, so I could not save the form. I was going to bypass this behavior by adding the patient and study fields as hidden, but then I read about instance and it seemed like a simpler solution.

    – Ambrose
    Jan 4 at 13:42



















  • Hi Chiefir. Thank you for the answer. This almost solved my issue, but initial does not play well with bound forms. My solution based off this was to use ... else: instance = StudentAnswer.objects.filter(student_id=patient.pk, study=study.pk).first(); form = ViewStudyForm(instance=instance)

    – Ambrose
    Jan 3 at 23:15













  • What went wrong with my variant?

    – Chiefir
    Jan 4 at 7:14











  • initial filled the participate field but left the ForeignKey fields for patient and study blank, so I could not save the form. I was going to bypass this behavior by adding the patient and study fields as hidden, but then I read about instance and it seemed like a simpler solution.

    – Ambrose
    Jan 4 at 13:42

















Hi Chiefir. Thank you for the answer. This almost solved my issue, but initial does not play well with bound forms. My solution based off this was to use ... else: instance = StudentAnswer.objects.filter(student_id=patient.pk, study=study.pk).first(); form = ViewStudyForm(instance=instance)

– Ambrose
Jan 3 at 23:15







Hi Chiefir. Thank you for the answer. This almost solved my issue, but initial does not play well with bound forms. My solution based off this was to use ... else: instance = StudentAnswer.objects.filter(student_id=patient.pk, study=study.pk).first(); form = ViewStudyForm(instance=instance)

– Ambrose
Jan 3 at 23:15















What went wrong with my variant?

– Chiefir
Jan 4 at 7:14





What went wrong with my variant?

– Chiefir
Jan 4 at 7:14













initial filled the participate field but left the ForeignKey fields for patient and study blank, so I could not save the form. I was going to bypass this behavior by adding the patient and study fields as hidden, but then I read about instance and it seemed like a simpler solution.

– Ambrose
Jan 4 at 13:42





initial filled the participate field but left the ForeignKey fields for patient and study blank, so I could not save the form. I was going to bypass this behavior by adding the patient and study fields as hidden, but then I read about instance and it seemed like a simpler solution.

– Ambrose
Jan 4 at 13:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008202%2fbooleanfield-not-updating-django%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'