On Mon, Feb 8, 2010 at 8:51 AM, mf <mf2...@gmail.com> wrote:

> I want to show the human-readable name for the type selected but I
> keep getting the stored value.
>
> TYPE_CHOICES = (
>        ('0', 'Basic'),
>        ('1', 'Full'),
>        ('2', 'Intermediate'),
> )
>
> class ServiceType(models.Model):
>        type = models.IntegerField(max_length=1, choices=TYPE_CHOICES)
>        amount = models.DecimalField(max_digits=10, decimal_places=2)
>
>        def __unicode__(self):
>                return '%s' % (self.get_type_display())
>

The model's type field is an IntegerField yet the actual values in the
choice tuples are strings. This works OK for storing but fails to match when
the real value retrieved from the DB is an integer, since '1' == 1 evaluates
to False, for example.  Thus get_type_display() doesn't find any choice that
matches the retrieved value and falls back to returning the actual value.
Switch the '0', '1', '2' in TYPE_CHOICES to 0,1,2, and it will work.

Karen

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to