Wednesday, October 8, 2014

display text value of a positiveintegerfield

I am a newbie. I have searched the django docs for this answer, but I failed to find a reference, so here is my question.

I have a select list as part of a form stored in my models.py file as a positiveintegerfield, as shown below:
    
class AchievementDetails(models.Model, FillableModelWithLanguageVersion):
    SELECT_ACHIEVEMENT_TYPE = 0
    ACADEMIC_ACHIEVEMENT = 1
    COMMERCIAL_ACHIEVEMENT = 2
    PERSONAL_ACHIEVEMENT = 3
    PROFESSIONAL_ACHIEVEMENT = 4
    SPORTING_ACHIEVEMENT = 5
    OTHER_ACHIEVEMENT_TYPE = 6

    ACHIEVEMENT_TYPES = (
        (SELECT_ACHIEVEMENT_TYPE, _('Select Type')),
        (ACADEMIC_ACHIEVEMENT, _('Academic Achievement')),
        (COMMERCIAL_ACHIEVEMENT, _('Commercial Achievement')),
        (PERSONAL_ACHIEVEMENT, _('Personal Achievement')),
        (PROFESSIONAL_ACHIEVEMENT, _('Professional Achievement')),
        (SPORTING_ACHIEVEMENT, _('Sporting Achievement')),
        (OTHER_ACHIEVEMENT_TYPE, _('Other Achievement Type'))
    )

    ....
    achievement_type = models.PositiveIntegerField(choices=ACHIEVEMENT_TYPES, default=SELECT_ACHIEVEMENT_TYPE, validators=[MinValueValidator(1)])
    ....

In one of my templates I would like to display the text value of the achievement_type, but I can only get the number value of the users saved selection to be displayed.

Here is my views code, that returns only the numerical value of the users saved selection:

def achievement_details(request):
    ....
    for ad in achievement_details:
             ad.achievement_type_as_text = ad.achievement_type 
    ....

How do I display the text value of the saved select list value?

For example if the user has saved 2 as the achievement_type, how do I display Academic Achievement and not the number 2.

I have tried the following line of code, but this only returns (2, )

ad.achievement_type_as_text = AchievementDetails.ACHIEVEMENT_TYPES[ad.achievement_type]




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1b0df635-2d8a-47c4-a3d5-556132d20d32%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment