Skip to main content

Usage

from tortoise import Model, fields
from tortoise_factory import model_factory


class User(Model):
    id = fields.IntField(primary_key=True)
    first_name = fields.CharField(max_length=100)
    last_name = fields.CharField(max_length=100)
    email = fields.CharField(max_length=200)


class Product(Model):
    id = fields.IntField(primary_key=True)
    name = fields.CharField(max_length=200)
    description = fields.TextField(null=True, default=None)
    price = fields.FloatField()
    created_by = fields.ForeignKeyField(
        "models.User",
        on_delete=fields.CASCADE,
        related_name="products",
    )

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

Generalities

All returned models are saved in db, meaning they will all have a .id value, the same is true for any ForeignKeyField

Random values

To get a simple random product:

product = await model_factory(Product)

the created_by will be created automaticaly since it cannot be None, when possible, the factory will put None into optional fields.

Specify some fields

product = await model_factory(Product, name="something", created_by__name="Bob")

we use __ to go throught relations

Fill optional values

product = await model_factory(Product, _optionals=["description"])

now you will have a product.description wich won't be None

Release files for tortoise-factory 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tortoise-factory 0.1.0
File Size Uploaded
tortoise_factory-0.1.0.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tortoise-factory 0.1.0
File Interpreter ABI Platform
tortoise_factory-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.9 kB

Release files / tortoise_factory-0.1.0.tar.gz

Download URL tortoise_factory-0.1.0.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b351f8b8d9d3e4727e91e26af697b1b963cf64b660d51d9838fa6bda9580c428
BLAKE2b-256 checksum
How to use checksums
8beedba834b5fc25c2fb7f94c9674a9594673b029bc8dbdf1b85ffc95f3a71d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.6 Linux/6.10.8-arch1-1

Release files / tortoise_factory-0.1.0-py3-none-any.whl

Download URL tortoise_factory-0.1.0-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
21c9727b2ba83e905a9453f40dd9b20d79041cefadcfca7b0d234583c5f6eb94
BLAKE2b-256 checksum
How to use checksums
6edfbdb5145086944dffc6e400926832474eed0b559c2fa8e2ba3efcd6f567bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.6 Linux/6.10.8-arch1-1

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page