Getting display value for choices in model

1,077 views
Skip to first unread message

Alex Ezell

unread,
Jul 23, 2008, 11:29:17 PM7/23/08
to Django users
I have a model with choices setup like this:

CREDENTIALS_CHOICES = (
('LM', 'Licensed Mover'),
('IN', 'Insured'),
('DR', 'DOT Registered (Interstate)'),
)
credentials = models.TextField(max_length=2, blank=True, null=True,
choices=CREDENTIALS_CHOICES)

In my form, I create the form field like this:
credentials =
forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=Roadshare.CREDENTIALS_CHOICES)

Now, Django wants that field in the DB to be a text[] and not just a
text (this is postgres).

However, if in my view I do something like this to get the meaningful
value from the choices:

post.credentials = post.get_credentials_display()

I get this error:

Traceback:
File "/home/user/lib/python2.5/django/core/handlers/base.py" in
get_response
85. response = callback(request, *callback_args,
**callback_kwargs)
File "/home/user/webapps/str/myproject/posts/views.py" in view_post
108. post = format_values(post,share_model)
File "/home/user/webapps/str/myproject/posts/views.py" in
format_values
278. post.credentials = post.get_credentials_display()
File "/home/user/lib/python2.5/django/utils/functional.py" in _curried
55. return _curried_func(*(args+moreargs), **dict(kwargs,
**morekwargs))
File "/home/user/lib/python2.5/django/db/models/base.py" in
_get_FIELD_display
429. return force_unicode(dict(field.flatchoices).get(value,
value), strings_only=True)

Exception Type: TypeError at /view/truckshare/animal/offer/36/
Exception Value: list objects are unhashable

This is using the latest Django trunk. I just did svn up about two
hours ago.

Thanks for any pointers you can offer.

/alex

Mayank Dhingra

unread,
Jul 24, 2008, 12:57:24 PM7/24/08
to Django users
Hi,

I am not clear in what you are trying to achieve here. Can you
elucidate a bit more.
Otherwise I use the get_fieldname_display() while displaying choices
in templates(Insured etc in your case)
and for views i just save the form(newforms) and it saves the form in
codes (IN etc in your case).

Alex Ezell

unread,
Jul 24, 2008, 4:17:06 PM7/24/08
to django...@googlegroups.com
On Thu, Jul 24, 2008 at 11:57 AM, Mayank Dhingra <dhingra...@gmail.com> wrote:
 
I am not clear in what you are trying to achieve here. Can you
elucidate a bit more.
Otherwise I use the get_fieldname_display() while displaying choices
in templates(Insured etc in your case)
and for views i just save the form(newforms) and it saves the form in
codes (IN etc in your case).

Thanks for taking a look. I use this line:
post.credentials = post.get_credentials_display()

to get the value (like "Insured") in the "post" object before that "post" gets passed to the template.

The problem is that get_fieldname_display() fails if the database column type is "text[]." However, it seems that Django forms wants to save a "forms.CheckboxSelectMultiple" as "text[]" in the DB.

So, the forms' expected DB column type doesn't seem compatible with get_fieldname_display().

Thanks again for looking.

/alex
Reply all
Reply to author
Forward
0 new messages