A helpful decorator for choice fields (Django choices or SQLAlchemy ChoiceType)
Project description
A helpful decorator for choice fields (Django choices or SQLAlchemy ChoiceType). Do choices the pythonic way.
Why create choices?
I got sick and tired of using choice fields in Django ORM and SQLAlchemy. Look here for a context
For example:
# 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:
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:
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!
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
Built Distribution
File details
Details for the file orm-choices-0.1.0.tar.gz
.
File metadata
- Download URL: orm-choices-0.1.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 131210476d4b5f1731047498102d5849eda080d6e05ea11155ae55ae8ea57592 |
|
MD5 | ba72130c2000e506a9e832bc84348bdf |
|
BLAKE2b-256 | 89e53d927a25f5e1cc8b9186a4471f5fa2d0bc347d52181009f2bd9ab5e0a744 |
File details
Details for the file orm_choices-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: orm_choices-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d52ec4f842115d1381ea3ab74b5eb8135faf6cccc203a751e9443346f30e393c |
|
MD5 | d0829662edc068ff08e354e09b89c828 |
|
BLAKE2b-256 | f033d8bc90c2805450713d600edde4e4a9eb9d85c95ef597e0976f7aaa57f758 |