On Oct 15, 1:51 pm, onno <[EMAIL PROTECTED]> wrote:
> The most annoying thing about using:
>
> TYPE = (('1', 'foo'),('2', 'BAR'))
> type = models.IntegerField(choices=TYPE)
>
> Is that you can't do this in your views when you need to select
> something
>
> Foo.objects.filter(type='BAR')
>
> you have to remember what number you gave it or make some "def" to
> handle it...
>
> Does anybody know a more easy way?

Have you thought about using more meaningful key values?  E.g.,

TYPE = (('FOO', 'foo display'), ('BAR', 'bar display'))

If you're stuck with magic numbers as keys you could always flip the
tuples for key lookup by value:

TYPE_KEYS = dict([(v,k) for k,v in TYPE])
Foo.objects.filter(type=Foo.TYPE_KEYS['foo display'])

You could also hang code-friendly values off your Foo object so you
could do:

class Foo(Model):
    foo_display = TYPE_KEYS['foo display']
Foo.objects.filter(type=Foo.foo_display)


I'd push for the more readable keys.


doug.


--~--~---------~--~----~------------~-------~--~----~
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