tortoise / tortoise/tortoise-orm

help,pydantic_model_creator con't display to reponse_model output foreignkey_value.

Open
#1,257 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

models.py:

class PostCategory(Model):
    """category model"""
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=50)
    published = fields.BooleanField(default=True)
    description = fields.TextField(max_length=300, null=True)

    parent: fields.ForeignKeyNullableRelation['PostCategory'] = fields.ForeignKeyField('models.PostCategory',
                                                                                       on_delete=fields.CASCADE,
                                                                                       null=True,
                                                                                       related_name='children')
    children = fields.ReverseRelation["PostCategory"]
    posts: fields.ReverseRelation["Post"]

    class Meta:
        table = "post_category"

    def __str__(self) -> str:
        return self.name

    class PydanticMeta:
        backward_relations = False
        exclude = ('posts', 'parent')
        allow_cycles = True
        max_recursion = 3

schemas.py

class CreateCategory(PydanticModel):
    name: str
    parent_id: int = None
    discription: str = None


GetCategory = pydantic_model_creator(models.PostCategory, name='GetCategory')


class OutCategory(GetCategory):
    id: int
    name: str

router.py

@category_router.post('/', response_model=schemas.OutCategory)
async def create_category(schema: schemas.CreateCategory, user: models.User = Depends(get_superuser)):
    return await service.category_s.create(schema)


@category_router.get('/', response_model=List[schemas.OutCategory])
async def get_all_category():
    return await service.category_s.filter(parent_id__isnull=True)

swagger output:
image

i can't to get parent category and children categories to show,how can i fix?

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 with models.py, schemas.py, and router.py, focusing on pydantic_model_creator and the response_model declarations. Reproduce the category endpoints with the shown parent and children relations, then inspect the generated response schema and returned queryset. Done means the intended parent and children category fields appear in the API output.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.