- Author:
- mallipeddi
- Posted:
- November 2, 2007
- Language:
- Python
- Version:
- .96
- Score:
- 2 (after 2 ratings)
This solves the problem with choices described here
Define your choices like this (in models.py):
statuses = MyChoices(BIDDING_STARTED=10, BIDDING_ENDED=20)
And then:
status = models.IntegerField(
default=statuses.BIDDING_STARTED,
choices=statuses.get_choices()
)
1 2 3 4 5 6 7 8 9 10 11 | class MyChoices:
def __init__(self, **entries):
self.__dict__.update(entries)
self.choices = [ ]
for val in entries.values():
for key in entries.keys():
if entries[key] == val:
self.choices.append((val, key))
def get_choices(self):
return self.choices
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Here is a similar solution which allows you to pass dictionaries to the constructor, and inserts the key/value pairs into the choice-list in the same manner they are in the dictionaries.
#
Please login first before commenting.