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.1.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.1-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.1.tar.gz.

File metadata

  • Download URL: django_bit_enum_list_field-0.1.1.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.4.0

File hashes

Hashes for django_bit_enum_list_field-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3ae0e6ebc409d93a06c5fc31428594c91fc8546d5ce2fb30700949baf641931b
MD5 a89ad179a5a8a60689dff369fe67af04
BLAKE2b-256 aefc0b790dd41cfc2929ff630104b2ade9e01b28c78a557fd0d25d8e2e306317

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_bit_enum_list_field-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dfa444bcb398584abcaf9b527bba585456138d3f6c775f0380e313f106dc3f9c
MD5 cb121f36e56287cca3d40c197acedb1e
BLAKE2b-256 979083ce4fa7098aad9d67ad03b83a37b2dab119f31801a1ea9b28e9620326c9

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