GithubHelp home page GithubHelp logo

zaharazod / django-bitfield Goto Github PK

View Code? Open in Web Editor NEW

This project forked from disqus/django-bitfield

0.0 2.0 0.0 190 KB

A BitField extension for Django Models

Home Page: http://code.disqus.com

License: Apache License 2.0

Makefile 0.78% Python 99.22%

django-bitfield's Introduction

django-bitfield

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

Changelog

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.

django-bitfield's People

Contributors

dcramer avatar mateuszmandera avatar mattrobenolt avatar alex avatar strycore avatar timabbott avatar ortholeviator avatar vad avatar streeter avatar christian-oudard avatar anton-ryzhov avatar bendavis78 avatar bmihelac avatar flyingeek avatar phillipberndt avatar andersk avatar bretthoerner avatar byk avatar dgouldin avatar dickbrouwer avatar ianawilson avatar coagulant avatar teferi avatar zaharazod avatar rystan avatar nicals avatar seanhayes avatar timgates42 avatar yuriiz avatar donald-m-ritter avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.