Skip to main content

django-bitfield

https://api.travis-ci.org/disqus/django-bitfield.png?branch=master

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!)

Requirements

  • Django >= 1.10.8

  • PostgreSQL (see notes)

Notes:

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

  • MySQL fails on most queries related to BitField’s.

Installation

Install it with pip (or easy_install):

pip install django-bitfield

Usage

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=(
        'awesome_flag',
        'flaggy_foo',
        'baz_bar',
    ))

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)
MyModel.objects.filter(pk=o.pk).update(flags=F('flags').bitor(MyModel.flags.awesome_flag))

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

# Remove awesome_flag (does not work in SQLite)
MyModel.objects.filter(pk=o.pk).update(flags=F('flags').bitand(~MyModel.flags.awesome_flag))

# Find by awesome_flag
MyModel.objects.filter(flags=MyModel.flags.awesome_flag)

# Exclude by awesome_flag
MyModel.objects.filter(flags=~MyModel.flags.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')

Enjoy!

Admin

To use the widget in the admin, you’ll need to import the classes and then update or create a ModelAdmin with these formfield_overrides lines in your admin.py:

from bitfield import BitField
from bitfield.forms import BitFieldCheckboxSelectMultiple

class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
            BitField: {'widget': BitFieldCheckboxSelectMultiple},
    }

admin.site.register(MyModel, MyModelAdmin)

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

Changelog

2.1.0 - 2021-05-25:

  • Add support for Django 3.1, 3.2 (No changes needed).

  • Add support for Python 3.8, 3.9.

  • Fixed multiple bugs with use in the Django admin.

  • Removed dead compatibility code.

2.0.1 - 2020-01-25:

  • Add support for Django 3.0.

2.0.0 - 2020-01-24:

  • Drop support for Django versions below 1.10.

  • Use _meta.private_fields instead of deprecated _meta.virtual_fields in CompositeBitField.

  • Add testing with python 3.6, 3.7 and Django 2.x to travis configuration.

Metadata

Release files for django-bitfield-4-0 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-bitfield-4-0 2.1.0
File Size Uploaded
django-bitfield-4-0-2.1.0.tar.gz 17.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-bitfield-4-0 2.1.0
File Interpreter ABI Platform
django_bitfield_4_0-2.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 35.6 kB

Release files / django-bitfield-4-0-2.1.0.tar.gz

Download URL django-bitfield-4-0-2.1.0.tar.gz
Size 17.7 kB
Tags Source
SHA-256 checksum
How to use checksums
156f48aaeb3f50b96a6fca4cadc3abbc36d23e887e12b0cc558da1fe0808c7cd
BLAKE2b-256 checksum
How to use checksums
c57e8a0cefdf2f6b214d5ed46b594aae523795040314f409dd97c291532ad78c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.4.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.7

Release files / django_bitfield_4_0-2.1.0-py2.py3-none-any.whl

Download URL django_bitfield_4_0-2.1.0-py2.py3-none-any.whl
Size 18.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
cf81a6ce432cf3f4c9be356ee56b756841f3ae526d1031482102b2ccc5491e1f
BLAKE2b-256 checksum
How to use checksums
1c5208450954622c011578e96a3e9aa8d250ef925fca27b54b538fb683ba9b25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.4.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.7

Release history Release notifications | RSS feed

This release

2.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page