I come with a really strange Django 1.4 behaviour wich sounds like a bug to me. I looked anywhere if someone experienced it, but didn't find anything.
My concern is about Django form creation using ModelForm, with a model containing boolean field.
Let Post and Topic two models wich should look like :
class Topic(models.Model):
my_bool = models.BooleanField(default=False)
class Post(models.Model):
topic = models.ForeignKey(Topic, related_name='posts')
Now, I'm gonna try to generate a form using TopicForm a class definied elsewhere :
post = Post.objects.get(pk=id_post)
f = TopicForm(instance=post.topic)
print f
It works fine : if in DB my_bool is False, the checkbox is not checked. If my_bool is True, the checkbox is checked. Here is the result :
<p><label for="id_my_bool">my_bool:</label> <input type="checkbox" name="my_bool" id="id_my_bool" /></p>
The problem !
OK, so here is the problem : if I use get_object_or_404 to get my Post object, then my_bool field will always be checked ! Look at this :
post = get_object_or_404(Post.objects.select_related('topic'), pk=id_post)
f = TopicForm(instance=post.topic)
print f
Output (weird) :
<p><label for="id_my_bool">my_bool:</label> <input checked="checked" type="checkbox" name="my_bool" value="0" id="id_my_bool" /></p>
Nota : I tryed several other ways to get the Post object, which all work fine :
post = Post.objects.filter(pk=id_post)[0]post = get_object_or_404(Post, pk=id_post)post = Post.objects.get(pk=id_post)
The only thing which makes it bug is :
post = get_object_or_404(Post.objects.select_related('topic'), pk=id_post)
Comments
- With Django 1.3, there wasn't any problem with it : only Django 1.4 makes it bug.
- I found some Django 1.4 bugs which seemed to be related, like :
- I do not use MySQL GIS backend, but
django.db.backends.mysql.
Have you got any idea ?
Thank you very much in advance !
Had the same bug in my app and found opened ticket. It seems the main problem is in pair select_related & MySQL: MySQL returns integers for booleans but Django can't associate them with corresponding model when using select_related; consequently, CheckboxInput gets value 0 instead of False and treats it like value in checkbox list.
You can:
or patch CheckboxInput widget: