Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dhilipsiva/orm-choices
Helpful decorators + utils for choice fields (Django choices or SQLAlchemy ChoiceType). Do choices the pythonic way.
https://github.com/dhilipsiva/orm-choices
choice choice-fields decorators django django-choices orm-choices pythonic sqlalchemy
Last synced: 3 months ago
JSON representation
Helpful decorators + utils for choice fields (Django choices or SQLAlchemy ChoiceType). Do choices the pythonic way.
- Host: GitHub
- URL: https://github.com/dhilipsiva/orm-choices
- Owner: dhilipsiva
- License: mit
- Created: 2015-11-26T08:32:12.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T02:12:35.000Z (about 2 years ago)
- Last Synced: 2024-10-11T10:26:54.256Z (3 months ago)
- Topics: choice, choice-fields, decorators, django, django-choices, orm-choices, pythonic, sqlalchemy
- Language: Python
- Homepage: https://pypi.python.org/pypi/orm-choices
- Size: 29.3 KB
- Stars: 13
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ORM Choices
Helpful decorators + utils for choice fields (Django choices or SQLAlchemy ChoiceType). Do choices the pythonic way.
## Why create ORM Choices?
I got sick and tired of using `choice` fields in Django ORM and SQLAlchemy. [Look here for a context](https://github.com/pythonindia/junction/issues/302)
For example:
```python
# Conference Application Choice Fields
CONFERENCE_STATUS_ACCEPTING_CFP = "Accepting Proposals"
CONFERENCE_STATUS_CLOSED_CFP = "Proposal submission closed"
CONFERENCE_STATUS_ACCEPTING_VOTES = "Accepting Votes"
CONFERENCE_STATUS_SCHEDULE_PUBLISHED = "Schedule Published"CONFERENCE_STATUS_LIST = ((1, CONFERENCE_STATUS_ACCEPTING_CFP),
(2, CONFERENCE_STATUS_CLOSED_CFP),
(3, CONFERENCE_STATUS_ACCEPTING_VOTES),
(4, CONFERENCE_STATUS_SCHEDULE_PUBLISHED),
)# Using it in a model:
class Conference(Model):
status = models.PositiveSmallIntegerField(
default=1, choices=CONFERENCE_STATUS_LIST)
```I have no Idea what 1 is (I mean its not really obvious that it means `CONFERENCE_STATUS_ACCEPTING_CFP` when `CONFERENCE_STATUS_LIST` is declared in some other file).
I needed a clean and DRY way of making use of Choice Fields.
Introducing `choices`:
```python
from orm_choices import choices
@choices
class ConferenceStatus:
class Meta:
ACCEPTING_CFP = [1, "Accepting Proposals"]
CLOSED_CFP = [2, "Proposal submission closed"]
ACCEPTING_VOTES = [3, "Accepting Votes"]
SCHEDULE_PUBLISHED = [4, "Schedule Published"]# Using it in a model:
class Conference(Model):
status = models.PositiveSmallIntegerField(
default=ConferenceStatus.ACCEPTING_CFP,
choices=ConferenceStatus.CHOICES)
```What did just happen? Crazy (not really). I know, right! Declare all your variables in `Meta` class (within `ConferenceStatus`). And apply the `orm_choices` decorator to `ConferenceStatus` class. And boom! Your `ConferenceStatus` now has these attributes:
```python
ConferenceStatus.ACCEPTING_CFP # This will return `1`
ConferenceStatus.ACCEPTING_VOTES # This will return `2`# And so on...
```
And it will add a new `CHOICES` attribute too.```
print(ConferenceStatus.CHOICES)
# Will Print
((1, "Accepting Proposals"), (2, "Proposal submission closed"), (3, "Accepting Votes"), (4, "Schedule Published"))
```Clean and DRY!
## Utils
### Curreny Codes
```python
from orm_choices.utils.currency_code import CurrencyCode
```### Blood Types
```python
from orm_choices.utils.blood_type import BloodType
```