1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
Metadata-Version: 2.1
Name: django-bitfield
Version: 2.2.0
Summary: BitField in Django
Home-page: https://github.com/disqus/django-bitfield
Author: Disqus
Author-email: opensource@disqus.com
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: Operating System :: OS Independent
Classifier: Topic :: Software Development
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Description-Content-Type: text/x-rst
Provides-Extra: tests
License-File: LICENSE
django-bitfield
---------------
.. image:: https://github.com/disqus/django-bitfield/actions/workflows/ci.yml/badge.svg
:target: https://github.com/disqus/django-bitfield/actions
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.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.
|