django-bitfield
Provides a BitField like class (using a BigIntegerField) for your Django models.
Requirements
Django >= 1.11 (a newer version with current security support is highly recommended).
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.4.0 - 2026-08-02:
Add support for Python 3.14.
2.2.0 - 2022-07-11:
Add support for Django 4.0.
Drop support for Django versions older than 1.11.29.
Drop support for Python 2.7.
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 2.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_bitfield-2.4.0.tar.gz | 14.9 kB | Without provenance | Details for django_bitfield-2.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_bitfield-2.4.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for django_bitfield-2.4.0-py2.py3-none-any.whl |
Total release size: 29.2 kB
Release files / django_bitfield-2.4.0.tar.gz
| Download URL | django_bitfield-2.4.0.tar.gz |
|---|---|
| Size | 14.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
64449791a5d84aaca02fe83f35a0641fd7e22f35bd46e95a3e4b7ed995235888
|
|
BLAKE2b-256 checksum How to use checksums |
1aed64f7bfd0f793770cb8ee243e288081ef2debd2e5b04da92a1d78c47f80f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / django_bitfield-2.4.0-py2.py3-none-any.whl
| Download URL | django_bitfield-2.4.0-py2.py3-none-any.whl |
|---|---|
| Size | 14.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
fc7cdc2ef891e7aedac72844b604f6b723e0859e1d20fb406389175bdcaeac4b
|
|
BLAKE2b-256 checksum How to use checksums |
8ab93551b8e353fb31004c272753787014d4ef1a5309aee1636357039be28e5d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Security for django-bitfield 2.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.