skip to navigation
skip to content

Not Logged In

django-bitfield 1.7.0

BitField in Django

Latest Version: 1.7.1

Provides a BitField like class (using a BigIntegerField) for your Django models.

(If you’re upgrading from a version before 1.2 the API has changed greatly and is backwards incompatible!)


  • Django >= 1.2

Note: SQLite does not support save operations using a Bit (per the example under Usage)


Install it with pip (or easy_install):

pip install django-bitfield


First you’ll need to attach a BitField to your class. This acts as a BigIntegerField (BIGINT) in your database:

from bitfield import BitField

class MyModel(models.Model):
    flags = BitField(flags=(

Flags can also be defined with labels:

class MyModel(models.Model):
    flags = BitField(flags=(
        ('awesome_flag', 'Awesome Flag!'),
        ('flaggy_foo', 'Flaggy Foo'),
        ('baz_bar', 'Baz (bar)'),

Now you can use the field using very familiar Django operations:

# Create the model
o = MyModel.objects.create(flags=0)

# Add awesome_flag (does not work in SQLite)

# Set flags manually to [awesome_flag, flaggy_foo]
MyModel.objects.filter( | MyModel.flags.flaggy_foo)

# Remove awesome_flag (does not work in SQLite)

# Find by awesome_flag

# Exclude by awesome_flag

# Test awesome_flag
if o.flags.awesome_flag:
    print "Happy times!"

# List all flags on the field
for f in o.flags:
    print f

# Get a flag label
print o.flags.get_label('awesome_flag')



To use the widget in the admin, you’ll need to update your ModelAdmin. Add the following lines to your ModelAdmin:

formfield_overrides = {
        BitField: {'widget': BitFieldCheckboxSelectMultiple},

Make sure you’ve imported the classes by adding these lines to the top of the file:

from bitfield import BitField
from bitfield.forms import BitFieldCheckboxSelectMultiple

There is also a BitFieldListFilter list filter (Django 1.4 or newer). To use it set list_filter ModelAdmin option:

list_filter = (
        ('flags', BitFieldListFilter,)

BitFieldListFilter is in bitfield.admin module:

from bitfield.admin import BitFieldListFilter
File Type Py Version Uploaded on Size
django-bitfield-1.7.0.tar.gz (md5) Source 2014-05-10 14KB
django_bitfield-1.7.0-py2.py3-none-any.whl (md5) Python Wheel 2.7 2014-05-10 12KB
  • Downloads (All Versions):
  • 331 downloads in the last day
  • 2041 downloads in the last week
  • 6942 downloads in the last month