BitField in Django
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-ivc-bitfield-1.9.7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | abb842e625dc4878887f11fc8b223c96cf9c8b01615fb02c0d624f75fc25ef59 |
|
MD5 | ff730de81890dd391f1be630db770309 |
|
BLAKE2b-256 | fab06bc80026b7457cdce831ffc6c99358f9a9c40225caa60c2dd9ad9567637d |
Hashes for django_ivc_bitfield-1.9.7-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19699b7b146504f9cd5c7a98d7b464d579b397c146072e93626ff49ea848ac52 |
|
MD5 | e48bc28c5f54321ed7aad4de26050c15 |
|
BLAKE2b-256 | 3ce03366ed4962662e3095d533b20c975aec8bda79cb28e199e15672fdb95f38 |