Skip to content

get_readable + Enum, not fetching enum names but values #1158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Day0Dreamer opened this issue Mar 1, 2025 · 1 comment
Open

get_readable + Enum, not fetching enum names but values #1158

Day0Dreamer opened this issue Mar 1, 2025 · 1 comment

Comments

@Day0Dreamer
Copy link

Following the documentation

I've managed to get it working.

class Build(Table):
    class DevelopmentStage(int, Enum):
        development = 1
        staging = 2
        release_candidate = 3
        release = 4

    id = Serial(primary_key=True)
    application = ForeignKey(references=Application, required=True)
    plugin_version = Varchar(length=255, index=True)
    release_date = Date()
    download_url = Varchar(length=1024)

    development_stage = SmallInt(choices=DevelopmentStage)

    @classmethod
    def get_readable(cls):
        return Readable(
            template="%s: %s (%s)",
            columns=[
                cls.application.name,
                cls.plugin_version,
                cls.development_stage,
            ],
        )

And yet, when running get_readable()
I get Cinema4D: 0.0.1 (1)
instead of Cinema4D: 0.0.1 (development)

And there is no way around that.

@dantownsend
Copy link
Member

Yeah, get_readable is done entirely in the database - so in this case it uses the numbers instead of the enum keys.

If it's not too late, you could try changing your enum to this instead:

class DevelopmentStage(str, Enum):
    development = 'development'
    staging = 'staging'
    release_candidate = 'release candidate'
    release = 'release'

We could modify get_readable so instead of just accepting a template and columns, it can accept any querystring - that would at least give you to ability to do a CASE WHEN:

@classmethod
def get_readable(cls):
    return Readable(
        template="%s: %s (%s)",
        columns=[
            cls.application.name,
            cls.plugin_version,
            QueryString("CASE WHEN development_stage = 1 THEN 'development' ... END")
        ],
    )

Maybe we could add some kind of utility to Piccolo for doing this automatically:

@classmethod
def get_readable(cls):
    return Readable(
        template="%s: %s (%s)",
        columns=[
            cls.application.name,
            cls.plugin_version,
            cls.development_stage.as_choices_keys()
        ],
    )

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants