#15040 closed Bug (fixed)
Boolean fields return 0 and 1 when loaded through select_related
Reported by: | homm | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.4 |
Severity: | Normal | Keywords: | |
Cc: | benjaoming@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This behavior only on mysql. SQLite always return True or False.
Models.py file:
from django.db import models class BoolModel(models.Model): bool = models.BooleanField() class Parent(models.Model): true = models.ForeignKey(BoolModel, related_name='trues') false = models.ForeignKey(BoolModel, related_name='falses')
In command line:
./manage.py shell >>> from ex.models import * >>> true = BoolModel.objects.create(bool=True) >>> false = BoolModel.objects.create(bool=False) >>> parent = Parent.objects.create(true=true, false=false) >>> parent = Parent.objects.get() >>> parent.true.bool True >>> parent.false.bool False >>> parent = Parent.objects.select_related('true', 'false').get() >>> parent.true.bool 1 >>> parent.false.bool 0
As you can seen, in first case, when parent object loaded without select_related, booleans is booleans.
In second case, when object loaded with select_related, boolens are numbers.
Version 1.2 and 1.3 acts the same.
Attachments (2)
Change History (19)
comment:1 by , 14 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 13 years ago
milestone: | 1.3 |
---|---|
Severity: | → Normal |
Type: | → Bug |
comment:6 by , 13 years ago
Has patch: | set |
---|
comment:7 by , 12 years ago
I just spent a ton of time debugging to figure out what this bug is. It causes ModelForms to have checkboxes that are all checked.
comment:8 by , 12 years ago
Cc: | added |
---|
*bump* there is a patch for this! It even contains a very neat set of tests. Can it be merged and the bug closed? :)
comment:9 by , 12 years ago
Version: | 1.2 → 1.4 |
---|
In my opinion this is a fairly significant issue. I spent all morning trying to figure out why a ModelForm (which was working perfectly locally on SQLite) had broken checkbox fields when deployed with a mysql backend.
parent = ParentModel.objects.select_related('child').get(pk=id) # BooleanFields won't work with this form if using MySQL because their values are rendered as ints due to select_related() child_form = ChildModelForm(instance=parent.child)
If there's no easy fix to this in Django itself I think there should at least be a warning in the documentation letting users know of this issue.
comment:10 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:12 by , 12 years ago
Resolution: | fixed |
---|---|
Status: | closed → new |
I am having this problem in 1.5.1 -- 0 when using select related, but False when not. I am using mysql.
by , 11 years ago
Attachment: | 15040-anon-test.diff added |
---|
comment:14 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
@anonymous, please open a new ticket with a test case if you can reproduce the issue on master. I tried the attached test based on your vague description but it passes on master and 1.5.x.
comment:15 by , 11 years ago
follow-up: 17 comment:16 by , 10 years ago
I apologise if this isn't the proper place for this comment, but what would it take to get this fix backpatched to 1.4?
comment:17 by , 10 years ago
Replying to kylegibson:
I apologise if this isn't the proper place for this comment, but what would it take to get this fix backpatched to 1.4?
I'm afraid it won't be. 1.4 is in security-fix-only mode.
This is because MySQL has special handling for returning boolean values; in 1.2, we added some callbacks to do the post-processing to ensure values get rectified. However, that rectification is performed with reference to the original model field. When using select_related, the original model obviously isn't being carried along for the ride.