Opened 17 years ago

Last modified 13 years ago

#7190 closed

BooleanField does not return <type: 'bool'> — at Version 2

Reported by: Jeffrey Froman Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: BooleanField, type
Cc: Leo Soto M. Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Ramiro Morales)

In some cases, a BooleanField returns an integer instead of a bool. The following example uses a MySQL backend:

# models.py
from django.db import models

class Simple(models.Model):
    b = models.!BooleanField()

$ ./manage.py syncdb
Creating table djest_simple
$ python
Python 2.5 (r25:51908, Sep 10 2007, 13:30:49)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from djest.models import Simple
>>> simple = Simple.objects.create(b=True)
>>> simple.b
True
>>> simple_2 = Simple.objects.get(pk=simple.pk)
>>> simple_2.b
1

This may not be a problem for normal usage, but makes testing, particularly using doctest, much less elegant.

Change History (4)

by Jeffrey Froman <django.tcijf@…>, 17 years ago

Attachment: boolfield.diff added

patch for django/db/models/fields/init.py

comment:1 by James Bennett, 17 years ago

This is kind of tricky, because under the hood in Python bool is a subclass of int that only ever has two instances (which test equal to 0 and 1 for False and True, respectively). Not all DBs actually store a boolean value, either; some store a 0 or a 1, and return that. Given that, and Python's general bent toward duck typing, I'm not sure whether we should strictly ensure that it always returns a bool instance.

comment:2 by Ramiro Morales, 17 years ago

Description: modified (diff)

by Pim Van Heuven, 17 years ago

Attachment: mysql.patch added
Note: See TracTickets for help on using tickets.
Back to Top