• Stars
    star
    378
  • Rank 113,272 (Top 3 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created almost 14 years ago
  • Updated 17 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A BitField extension for Django Models

django-bitfield

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.

More Repositories

1

gargoyle

Feature switches in Django
Python
747
star
2

nydus

Nydus is a Python toolkit for managing database connections and routing operations, primarily for Redis
Python
380
star
3

disqus-react

A React component for Disqus
JavaScript
369
star
4

DISQUS-API-Recipes

Cook all the things!
PHP
287
star
5

django-mailviews

Class-based email views for the Django framework, including a message previewer.
Python
269
star
6

overseer

A status board built with Django
Python
257
star
7

gutter

Fully featured Python feature switch toolkit
Python
226
star
8

nexus

A centralized, pluggable admin app for Django
Python
204
star
9

disqus-python

Disqus API bindings for Python
Python
162
star
10

python-phabricator

Python bindings for Phabricator
Python
159
star
11

django-db-utils

Utilities for your Django Database
Python
159
star
12

disqus-php

Disqus API bindings for PHP
PHP
146
star
13

django-modeldict

Python
132
star
14

sharding-example

Example of sharding tools used at Disqus
Python
112
star
15

django-perftools

Performance monitoring tools for Django
Python
110
star
16

backbone.uniquemodel

Backbone.js plugin for ensuring unique model instances across your app
JavaScript
98
star
17

porkchop

Simple HTTP-based system information server
Python
93
star
18

durabledict

Dictionary-style access to different types of models.
Python
85
star
19

postfix-stats

Simple threaded stats aggregator for Postfix
Python
76
star
20

grunt-smartrev

A "smart" file versioner for production environments which takes inter-file dependencies into account automatically.
JavaScript
75
star
21

orbital

Orbital is a real-time map built on top of Websockets and ZeroMQ.
JavaScript
69
star
22

disqus-wordpress

Legacy WordPress plugin 1.x - 2.x. Go to disqus/disqus-wordpress-plugin for current version -
PHP
68
star
23

toronado

fast lxml-based stylesheet inliner
Python
60
star
24

disqus-install-examples

Installation Examples for Disqus
HTML
55
star
25

mule

Mule is a distributed test runner for Python
Python
51
star
26

gutter-django

Python
45
star
27

menagerie

ZooKeeper-backed Django settings interface.
Python
42
star
28

disqus-postgres

Python
39
star
29

fpm-recipes

Makefile
39
star
30

grockets

Realtime streaming graphite data via socket.io and node.js
JavaScript
38
star
31

nose-performance

A plugin for Nose for running performance tests
Python
34
star
32

zumanji

A web interface for aggregating results from nose-performance
JavaScript
33
star
33

disqus-wordpress-plugin

WordPress plugin for Disqus (Latest version)
TypeScript
33
star
34

nagios-plugins

Nagios plugins written at Disqus
Python
30
star
35

channels

A demo of a modern forum powered by DISQUS
JavaScript
30
star
36

nydus-django

Python
29
star
37

playa

An audio playing web service
Python
27
star
38

sentry-graphite

An extension for Sentry that increments a key in graphite based on the error.
Python
27
star
39

disqus-arcanist

Arcanist extensions by Disqus
PHP
26
star
40

php-disqus

Old PHP bindings for Disqus
PHP
22
star
41

porkchop-plugins

Plugins for Porkchop
Python
18
star
42

nose-socket-whitelist

nose plugin that provides control over what socket connections can be opened during test execution
Python
17
star
43

disqus.github.com

Python
16
star
44

codebox

Code Sharing for Teams
Python
14
star
45

nose-template-usage

Template usage reports for testing Django applications with Nose.
Python
13
star
46

mockhttp

Python
13
star
47

django-patches

A collection of patches of various changes in Django used by Disqus
12
star
48

pgshovel

A change data capture system for PostgreSQL
Python
11
star
49

pg_audit

Audit PostgreSQL tables in an automated fashion.
10
star
50

docker-nginx

some dockerfiles for some nginx
Dockerfile
9
star
51

graphite-reporter-agent

Java Agent for Cassandra's Metrics' Graphite Reporter
Java
9
star
52

facebook-python-sdk

Facebook Platform Python SDK
Python
8
star
53

django-metaredirect

META-tag and JavaScript based generic redirect views for maintaining HTTP referrers.
Python
8
star
54

psycopg2-managed-connection

Thread-safe connection manager for psycopg2 connections.
Python
8
star
55

pg_partition

Partition management for PostgreSQL
7
star
56

fastly-slurper

Slurps delicious fastly logs. Mmm.
Python
7
star
57

django-test-helpers

Python
7
star
58

convey

Python
6
star
59

disqus-api-demos

Python
6
star
60

docker-pgqd

pgqd (PgQ Ticker Daemon) on Docker
Python
5
star
61

standup-helper

Disqus' standup bot + dashboard.
JavaScript
4
star
62

phab-trello

Phabricator-to-Trello Webhook
JavaScript
4
star
63

bokbok

JavaScript
4
star
64

imperium-python

Impermium API bindings for Python
Python
4
star
65

grunt-kssgen

KSS styleguide generator for grunt. - Alternative to grunt-kss since it is dead
JavaScript
4
star
66

diamond-collectors

Python
3
star
67

nginx-old

Nginx (PPA packaging)
C
2
star
68

nose-unittest

Python
2
star
69

datasketches-hive

Java
2
star
70

kazoo-eventlet-handler

Kazoo handler for usage in eventlet greenthreaded environments.
Python
2
star
71

smartrev

A "smart" file versioner for production environments which takes inter-file dependencies into account automatically.
2
star
72

trello-phab

Parse phab-trello webhook attachments
JavaScript
1
star
73

docker-heka

heka in a docker
1
star
74

Hive-JSON-Serde-tmp

Java
1
star
75

nginx

Disqus Nginx Build
C
1
star
76

facebook-sdk

Mirror of facebook-sdk
Python
1
star
77

dataweek2013

Scala
1
star
78

disqus-slack

post Disqus comments to a Slack channel or PM with /disqus <comment url>
JavaScript
1
star
79

kafka-python

mirror of original dpkp/kafka-python
Python
1
star