tortoise / tortoise/tortoise-orm

[IDEA] Alternate model syntax

Open
#71 8 comments 14 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

In trying to solve the DB → python type mapping, and the advent of typing, a possible solution is to define types that then get evaluated into both Python and DB types:

This also raises the question of a cleaner (more modern) model definition, by defining the type:

Since py3.6 we have the typed variables:

class Author(Model):
    name: str[255]
    books: reverse_fk[Book]

class Book(Model):
    title: str[255]
    author: Author
    published: datetime
    logo: bytes = None
    pages: int
    extra: JSONField = {}
    price: Decimal[18,5]
    created_at: datetime[auto_now_add]

Which is already standard in Py3.7 @dataclass

We could extend type definitions so we can build model definitions off of types.
e.g. str[100] = 'some default'CharField(max_length=100, default='some default')
The syntax is not comprehensive, but we could probably use type declarations for a large portion of use cases.

This would require Py3.6+ to function, so dropping Py3.5 & PyPy.

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 by reviewing the proposed Python 3.6+ typing and dataclasses syntax alongside the existing model definitions. The issue names no files or tests, so the scope, supported declarations, database mappings, compatibility policy, and completion criteria would need to be established before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.