Is there any easy way to do so?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can set the choices attribute to any iterable:
http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.choices
I haven’t tested this myself so I’m not sure when the choices attribute is actually evaluated, but you may be able to assign a generator function that would calculate your desired choices.
You might also investigate using the Model post_init signal: http://docs.djangoproject.com/en/1.1/ref/signals/#post-init
This will give you access to your model after Django has initialized it and so you could set the choices at that time. You’d probably want to go through the “_meta” interface like so: