tortoise / tortoise/tortoise-orm

Model printing requires the output of the primary key value

Open
#1,342 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

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__}>"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Model class and compare its current str behavior with repr. Verify the requested output for models with and without a primary-key value, and consider whether existing model representation tests cover both cases; done means printing includes the model name and, when present, its primary key.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.