Skip to main content

BitField in Django

Project description

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 - 2020-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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-bitfield-2.1.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

django_bitfield-2.1.0-py2.py3-none-any.whl (18.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-bitfield-2.1.0.tar.gz.

File metadata

  • Download URL: django-bitfield-2.1.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.9

File hashes

Hashes for django-bitfield-2.1.0.tar.gz
Algorithm Hash digest
SHA256 a55859fd16ce4269d5ceed3e20cf8fc3c2df866f0a78b90c60a19a0e76aa5fd8
MD5 592be058c4d15e5782af15fa4b019cbe
BLAKE2b-256 fd839415c6cdd77d6f7574ce55d9e7b8016f4194f290593c66e6e40592bcf69f

See more details on using hashes here.

File details

Details for the file django_bitfield-2.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_bitfield-2.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.9

File hashes

Hashes for django_bitfield-2.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 158f1056e22cce450d0a49633ea77bfd84b85a2294b1ef03faa775a485f4065d
MD5 55d21514f639a75227119098da495452
BLAKE2b-256 e7dcbfcf2b9cc5dc3832430f0ca39a36caaa771234fdd7601a15bd0c25d1178f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page