Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
.. 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.
Notes:
Bit
(per the example under Usage).Install it with pip (or easy_install)::
pip install django-bitfield
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!
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
2.2.0 - 2022-07-11:
2.1.0 - 2021-05-25:
2.0.1 - 2020-01-25:
2.0.0 - 2020-01-24:
FAQs
BitField in Django
We found that django-bitfield demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.