question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Is there a way to get the display instead of the value for a field with a choices parameter associated with it?

See original GitHub issue

Hello, everyone.

In the following code:

class Model(db.Model):
    field = pw.Charfield(
        choices=[1, 'foo']
    )

    def __unidoce__(self):
        return self.<api call I don't know about>

I would like to print foo instead of 1. The ORM implements a way to achieve this?

Thanks in advance.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
coleifercommented, Feb 3, 2015

It’s just Python, there’s no magic there. One easy way to accomplish this is to write something like this:


class Tweet(Model):
    STATUS_CHOICES = (
        (0, 'Draft'),
        (1, 'Published'),
        (9, 'Deleted'))
    status = IntegerField(choices=STATUS_CHOICES)

    def get_status_label(self):
        return dict(self.STATUS_CHOICES)[self.status]
0reactions
pavelikcommented, Mar 14, 2018

Ok, your solution works. Before it I have tried:

{% for order in orders %}
  {# status column is populated with the status value, so we can use the helper method to get label #}
  {{ order.id }} {{ order.status.get_status_label() }}
{% endfor %}

…but this not work. Your answer works as I expected.

Thank you very much and thanks for your work on peewee.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic or Conditional display of Jenkins job's parameters ...
In this example MY_PARAM is a parameter in the Jenkins job. As long as you put 'MY_PARAM' in the Active Choices 'Referenced Parameters'...
Read more >
Use parameters to ask for input when running a query
Create a parameter query · Create a select query, and then open the query in Design view. · In the Criteria row of...
Read more >
Use Parameters to Make Views More Interactive - Tableau Help
Tip: Instead of typing each value in the list, click Add values from > Parameters to add them from Select Column 1 Heading....
Read more >
How to put a value in a selection screen parameter
Hi,. Either u can pass the default value in parameter or u have to hard code in where clause of select statement i.e...
Read more >
Using a control with a parameter in Amazon QuickSight
The values display alphabetically in the control, unless there are more ... For parameter controls, the child control must be linked to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found