I’ve spent some time looking over other questions but I dont really understand some of the implementations. I’m creating a dummy book rental web app using django. I want to display a list of books with a checkbox to select it using django form.
Is there a way to dynamically generate the label of the checkbox from the title of books created in a separate model?
Example Book model:
class Book(models.Model):
title = models.CharField(max_length=100)
.
.
.
pub_date = models.DateField()
Rental Form:
class RentalForm(forms.Form):
name = forms.BooleanField(label = "label")
.
.
I want the form to dynamically insert the title of the book into the label field but I don’t know how.
Look into the ModelMultipleChoiceField and CheckboxSelectMultiple widget:
I think it uses __unicode__ to display the “label” next to the checkbox. You’ll need some logic on the .save() or POST request to handle this, but it should work.
Alternatively, if you want true BooleanFields, you’ll need to do something with __init__() on the RentalForm and create all the BooleanFields on the fly.