tortoise / tortoise/tortoise-orm

pydantic_model_creator can't see string references to other models.

Open
#573 1 comment 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

Describe the bug
When a ReverseRelation field is referenced on a model, and its defined by passing in the name of that ReverseRelation, pydantic_model_creator doesn't recognise the model. The Signal() class should be in another file, ie, app.models.signal
To Reproduce

class Signal(Model):
    foo='someting'

class User(Model):
    signals: fields.ReverseRelation["Signal"]

UserOut_Pydantic = pydantic_model_creator(
    User, name="UserOut",  include=("username", "photo_url"))

NameError: name 'Signal' is not defined

Expected behavior
I expect that pydantic_model_creator should recognise my Signal model, and if it doesn't, there should be a work around for passing in string references to ReverseRelation fields.

Additional context
Add any other context about the problem here.

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 pydantic_model_creator entry point and trace how ReverseRelation annotations are resolved when the related model is given as a string reference. Reproduce the NameError with the Signal and User example, then add coverage for models defined in separate modules and confirm model creation succeeds.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.