positiveintegerfield - display value in add template & remove value in edit template

31 views
Skip to first unread message

6233114 6233114

unread,
Oct 9, 2014, 12:34:33 AM10/9/14
to django...@googlegroups.com
I am a newbie and this question is sort of related to a question I asked not so long ago.

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 my edit template I would like to remove the (SELECT_ACHIEVEMENT_TYPE, _('Select Type')), option from the actual select list that is displayed to the user, but dsiplay the value when the add template is displayed to the user.

I have read that I can use val.pop(0), but I am really uncertain as to how I can apply this to the edit details of my views.py file, or even if I should be using val.pop(0).

Is someone able to direct me to the relevant reference in the django docs? As I have been unable to find the reference when I read & searched in the docs myself.

Thanks.

Collin Anderson

unread,
Oct 9, 2014, 8:48:36 AM10/9/14
to django...@googlegroups.com
hmm... you may need to override the form field on your form. I'm going to go out on a limb and use some soon-to-be documented syntax.

new_choices = [(id, v) for id, v in AchievementDetails.ACHIEVEMENT_TYPES if id != 0]
class AchievementDetailsForm(forms.ModelForm):
    achievement_type = AchievementDetails._meta.get_field('achievement_type').formfield(choices=choices)

Actually, better yet, are you using django 1.7? You can do this:
 - change SELECT_ACHIEVEMENT_TYPE = 0 to SELECT_ACHIEVEMENT_TYPE = None
- define your field like this:
    achievement_type = models.PositiveIntegerField(choices=ACHIEVEMENT_TYPES)



Reply all
Reply to author
Forward
0 new messages