Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/runekaagaard/django-multiple-choices
Django model field that can select multiple choices and stores them as a bitmask value in the database.
https://github.com/runekaagaard/django-multiple-choices
Last synced: 11 days ago
JSON representation
Django model field that can select multiple choices and stores them as a bitmask value in the database.
- Host: GitHub
- URL: https://github.com/runekaagaard/django-multiple-choices
- Owner: runekaagaard
- License: agpl-3.0
- Created: 2022-04-11T14:42:02.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-04-12T15:32:28.000Z (over 2 years ago)
- Last Synced: 2024-11-07T11:48:20.400Z (2 months ago)
- Language: Python
- Size: 41 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# django-multiple-choices
Alternative to https://github.com/disqus/django-bitfield and https://github.com/goinnn/django-multiselectfield/
- Works on MySQL (postgres not tested)
- Stores the selected value as a bitmask value which allows for fast db access
- Admin support
- Custom `mc_in` and `mc_notin` query lookups
- One file# Usage
Import it and stick it on a model:
```
from django.db import models
from multiple_choices import MultipleChoicesModelFieldLIKES = ((0, "Pizza"), (1, "Juice"), (2, "Beef"), (3, "Beer"), (4, "Milk"))
class Person(models.Model):
likes = MultipleChoicesModelField(choices=LIKES, required=True)
```Both the `choices` and the `required` props are required.
Two new query lookups `mc_in` and `mc_notin` are available:
```
Person.objects.filter(likes__mc_in={0, 1}) # Persons who like pizza and juice
Person.objects.filter(likes__mc_notin={4}) # Persons who doesn't like milk
```Exact searching goes like this:
```
Person.objects.filter(likes={2}) # Persons who only likes beef
```# Installation
Copy `multiple_choices.py` somewhere on your python path.