Tuesday, September 28, 2010

Re: Model field choices

On 9/28/2010 10:39 PM, Derek wrote:
> I have a model which has choices specified for the "offer_type" field:
>
> class Coupon(models.Model):
> offer_types = (
> (1, 'Percentage Off'),
> (2, 'Amount Off'),
> (3, 'Free'),
> )
>
> business = models.ForeignKey(Business)
> offer_type = models.IntegerField(choices=offer_types)
>
> How do I then retrieve the "human readable" values when referencing a
> Coupon object? Right now Coupon.objects.get(id=1).offer_type returns
> the numeric value only ("3" instead of "free"... which I suppose is to
> be expected).
>
> Thanks!
>
You should find that your model instances have a
get_offer_type_display() method that you can call to retrieve the value
that the user sees in the interface. These methods are added for each
field with choices set in its definition.

regards
Steve
--
DjangoCon US 2010 September 7-9 http://djangocon.us/

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

No comments:

Post a Comment