tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.55k stars 374 forks source link

Model printing requires the output of the primary key value #1342

Open Chise1 opened 1 year ago

Chise1 commented 1 year ago

Is your feature request related to a problem? Please describe. When I print a model,I can only got model's name. The str function should have the same logic as the repr function. In the past, every time I printed a model, I had to print an extra pk field. Describe alternatives you've considered Rewrite the Model's function:str like:


class Model(metaclass=ModelMeta):
    """
    Base class for all Tortoise ORM Models.
    """
    ...

    def __str__(self) -> str:
        if self.pk:
            return f"<{self.__class__.__name__}: {self.pk}>"
        return f"<{self.__class__.__name__}>"
bekha-io commented 1 year ago

That would be great! All other ORMs in comparison already have this feature.

DenisFrunza commented 1 year ago

Agree would be nice to see.

long2ice commented 1 year ago

PR welcome

DenisFrunza commented 1 year ago

I'll leave the PR here.

long2ice commented 1 year ago

I notice that there is __repr__ has the behavior

Chise1 commented 1 year ago

I thought repr should not affect str when refactored by the user, so I didn't call repr.