Skip to main content

A field for Django models to store lists of enums as integers

Project description

BitEnumListField for Django Models

[!WARNING]
This library is still work in progress. While it does work for case it was creaded for, it might not work in different contexts, please use at your own risk

This field is used to store a list of enum values as a binary representation on a database.

Example

First add the BitEnumListField to your model and choose the Enum it should use.

class Weekdays(Enum):
    Monday = 0
    Tuesday = 1
    Wednesday = 2
    Thursday = 3
    Friday = 4
    Saturday = 5
    Sunday = 6

    
class Event(models.Model):
    name = TextField()
    days = BitEnumListField(Weekdays)

It require the Enum values to be integers. At this point you can create new records just by setting the field to a list of enum values:

my_event = Event(
    name="Event",
    days=[Weekdays.Tuesday, Weekdays.Friday]
)

my_event.save()

This will set the days field on the database to 18. That's because Weekdays.Tuesday correspond to the bitwise mask 0100000 which, converted to decimal, gives us a 2 (digits are reversed when converting from binary to decimal, with hti swe don't have to worry about the length of the enumerator changing over time). Now for Weekdays.Friday we have the mask 0000100 that converts to 16. Now 0100000 | 0000100 equals 0100100 which converts to 18
At this point we can create, read, update and delete objects that use the BitEnumListField

Querying the database

Using the standard match in a django query will only return us the elements that match exactly that list of enum values.
If you want to create more complex queries this library introduces 3 lookups you can use.

All Lookup

This lookup will return all elements that have all the specified enum values (and eventually more)

Event.objects.filter(days__all=[Weekdays.Tuesday, Weekdays.Friday])

The example above will return all the events that have both tuesday and friday set as days.

Any Lookup

This lookup will return all elements that have at least one of the specified enum values (and eventually more)

Event.objects.filter(days__all=[Weekdays.Tuesday, Weekdays.Friday])

The example above will return all the events that have either tuesday or friday set as days. So for example it will return a day that has tuesday + wednesday.

None Lookup

This lookup will return all elements that don't have any of the specified values.

Event.objects.filter(days__nome=[Weekdays.Tuesday, Weekdays.Friday])

The example above will return all the events that have not tuesday nor friday set as days.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_bit_enum_list_field-0.1.0.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_bit_enum_list_field-0.1.0-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file django_bit_enum_list_field-0.1.0.tar.gz.

File metadata

  • Download URL: django_bit_enum_list_field-0.1.0.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.3.0

File hashes

Hashes for django_bit_enum_list_field-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b2a4656cb615b82c87a0e64f902b2dc27993b5f994e0e9e0fab74e2903a99283
MD5 73d78bd9d89253ea7ecb2f46233646a6
BLAKE2b-256 08611213468d060b750f2b136b2e9fcdf08bc2df626ff087b9482aa0ed295b45

See more details on using hashes here.

File details

Details for the file django_bit_enum_list_field-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_bit_enum_list_field-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 916f0e5be837922f12a1087ed25195f519329fd5d2e2a0eb029b714b2e8643f1
MD5 222441ef993889ebc6c1a5dde0f1fa7c
BLAKE2b-256 1dbc981c57e686a1bcc02cbad265d83831bdb00730dc25cc55412f444e5c889e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page