Skip to content
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

make Readable for boolean fields more user-friendly #741

Open
trondhindenes opened this issue Dec 29, 2022 · 5 comments · May be fixed by #742
Open

make Readable for boolean fields more user-friendly #741

trondhindenes opened this issue Dec 29, 2022 · 5 comments · May be fixed by #742

Comments

@trondhindenes
Copy link

As far as I can see, if using Readable and get_readable, boolean fields are represented as f and t (False and True). It would be good if it was possible to represent these in a more user-friendly way - Maybe even make the default similar to Python's string representation of booleans:

In [1]: str(True)
Out[1]: 'True'

In [2]: str(False)
Out[2]: 'False'

@sinisaos
Copy link
Member

@trondhindenes For boolean readable fields, Piccolo returns the database representation of the boolean column (Sqlite "0" or "1" using PRINTF, Postgres "f" or "t" using FORMAT). I think the easiest way to get a nicer display is to use Python and modify the results something like this

result = await Band.select(Band.get_readable())
for item in result:
    if item["readable"] in ["0", "f"]:
        item["readable"] = "False"
    else:
        item["readable"] = "True"
print(result)

and result is

[
   {
      "readable":"False"
   },
   {
      "readable":"True"
   }
]

I hope that's ok for you.

@trondhindenes
Copy link
Author

Hi thanks - I assumed that was the case.

I'm using readables with class methods to make piccolo-admin print nicer strings:

    @classmethod
    def get_readable(cls) -> Readable:
        return Readable(template="%s (expired: %s)", columns=[cls.name, cls.expired])

I just assumed there wasn't much I could do in there, but now that I actually read the code I can ofc manipulate the Readable before it's returned. I should have double-checked before opening the issue. Thanks!

@dantownsend dantownsend linked a pull request Dec 30, 2022 that will close this issue
@dantownsend
Copy link
Member

Hi - sorry for the slow reply, I managed to catch the flu over the holidays.

PR #742 offers another solution:

class DiscountCode(Table):
    code = UUID()
    active = Boolean(default=True, null=True)

    @classmethod
    def get_readable(cls) -> Readable:
        return Readable(
            template="%s - %s",
            columns=[
                cls.code,
                SelectRaw(
                    "CASE WHEN active THEN 'active' ELSE 'inactive' END"
                ),
            ],
        )

We added SelectRaw recently, so the PR makes it so you can use it with Readable.

What do you both think?

@sinisaos
Copy link
Member

@dantownsend This looks and works great and is nicer than manipulating the result.

@trondhindenes
Copy link
Author

Nice! This more than covers my use case. I'd love to see "advanced usage" snippets like this in the docs as well

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

Successfully merging a pull request may close this issue.

3 participants