Reputation: 1317
I am able to correctly save the value of "Private" in the model. But, when I open the Edit Page, it's always set to unchecked irrespective of the value being True or False.
# from forms.py
class MyEditForm(forms.Form)
title = forms.CharField(
label='Title',
widget=forms.TextInput(attrs={'size': 64})
)
private = forms.BooleanField(
label='Private',
required=False,
)
# from models.py
class MyData(models.Model):
title = models.CharField(max_length=200)
private = models.BooleanField()
# from views.py
def save_page(request)
try:
mydata = MyData.objects.get(
private=private
)
title = mydata.title
private = mydata.private
except ObjectDoesNotExist:
pass
form = MyEditForm({
'title': title,
'private': private
})
Upvotes: 0
Views: 360
Reputation: 5206
You should be using a ModelForm not a Form. Then when you instantiate the ModelForm set the instance to your model. This will also save you from needing to duplicate the form fields since django will auto generate the form fields based on the model.
foo = Foo.objects.get(id=foobar)
form = YourForm(instance=foo)
Upvotes: 1