tortoise / tortoise/tortoise-orm

OneToOne In Pydantic Model with pydantic_model_creator method

Open
#1,309 1 comment 1 reaction 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
I cannot use OneToOne relation field inside a pydantic model creator's method.

To Reproduce
When I try to create a pydantic model using pydantic_model_creator method, it ignores OneToOne field, as it doesn't exist. Further on, I wrote validation for the method, but raised error to not write validation that doesn't exist in the pydantic method. Also it's not included in swagger's document, so it's not detected as a field at all.

models.py

class Supplier(models.Model):
    id = fields.UUIDField(pk=True)
    user = fields.OneToOneField('models.Users', on_delete=fields.SET_NULL, null=True, related_name='supplier')
    comission = fields.DecimalField(max_digits=30, decimal_places=2)
    current_debt = fields.DecimalField(max_digits=30, decimal_places=2, default=0)
    last_time_refresh = fields.DatetimeField(null=True, blank=True)
    shared_proxies = fields.BooleanField(default=False)
    wallet = fields.TextField(null=True, blank=True, )
    is_vip = fields.BooleanField(default=False)
    server = fields.CharField(default='1.1.1.1', max_length=15)
SupplierPydanticIn = pydantic_model_creator(models.Supplier, name="SupplierIn", 
    include=('user', 'comission', 'current_debt', 'shared_proxies', 'server')
)

class SupplierIn(SupplierPydanticIn):
    @validator('user')
    def validate_user(cls, value):
        print("Just for test purpose!")
        return value

image

Expected behavior
should get related field's ID as in pydantic model

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

Reproduce the example from models.py using pydantic_model_creator with the Supplier.user OneToOneField and inspect the generated schema. Start at the pydantic_model_creator entry point and its relation-field handling; done means the related field appears in the Pydantic model, accepts the related ID, and is represented in the generated Swagger document.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.