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

  • 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 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

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-1.9.6.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

django_bitfield-1.9.6-py2.py3-none-any.whl (18.2 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: django-bitfield-1.9.6.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for django-bitfield-1.9.6.tar.gz
Algorithm Hash digest
SHA256 d32fc6610f80b0b17a832a487ae18860a563d9a9842259d0d37ae1e62a1854ab
MD5 899431f243f60f19883cd48d2af70423
BLAKE2b-256 9370fb1621a09fefbf180954f53a19c7765551d02736f4ba62bc58ed609e2b0c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_bitfield-1.9.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for django_bitfield-1.9.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e8d4dc8727d4d655f1f740771beb6566d1928f7270c1c020cf5af278784f2843
MD5 9dde6dca1a3dd0caf4254d252368edb1
BLAKE2b-256 3d051c86a54380f5ab57ee049c6f2b2ea81be7d2ecd6e40b08f1a4b88322b17e

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