On Tue, 2007-03-27 at 19:11 -0700, Alex Dong wrote:
> Hi Folks,
> 
> I'm wondering are there any "parse_FOO_display" method for the django
> models?
> 
> For example:   {{{
> class Transaction(models.Model):
>     STATUS = (
>             (1, 'Canceled_Reversal'),
>             (2, 'Completed'),
>             (3, 'Pending'),
>             (4, 'Refunded'),
>             (5, 'Reversed'),
>             (6, 'Processed'))
>     status      = models.PositiveSmallIntegerField(choices=STATUS)
> }}}
> 
> I know that I could call "tx.get_status_display()" to get descriptive
> name for the status, say "Completed". But how can I get `2` when given
> the descriptive name `Completed`?

There's no standard method to do that. It doesn't seem to be a very
common requirement. You could write one yourself if you wanted to:

        def get_less_readable_version(self, choice):
            choices = self._meta.get_field('status').choices
            for val, name in choices:
                if name == choice:
                    return val

Obviously a few variations on this theme are possible, such as putting
the choices in a dictionary once and then doing a lookup. The only
slightly tricky thing here is the _meta.get_field(...).choices call to
get the choices list for a particular field.

Regards,
Malcolm
        


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to