awtkns / fastapi-crudrouter

A dynamic FastAPI router that automatically creates CRUD routes for your models
https://fastapi-crudrouter.awtkns.com
MIT License
1.34k stars 156 forks source link

Support default_factory on primary keys of schemas for create routes #166

Open daxxog opened 1 year ago

daxxog commented 1 year ago

Pydantic has a way to specify a function that provides a default value for a field.

This pull request adds support for this in fastapi-crudrouter, for primary keys that utilize this.

Example:

from secrets import choice
from pydantic import BaseModel, Field
from fastapi import FastAPI
from fastapi_crudrouter import MemoryCRUDRouter as CRUDRouter

class Potato(BaseModel):
    id: int = Field(default_factory=lambda: choice(range(0,3)))
    color: str
    mass: float

app = FastAPI()
app.include_router(CRUDRouter(schema=Potato))
vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
fastapi-crudrouter ✅ Ready (Inspect) Visit Preview Jan 11, 2023 at 1:32PM (UTC)
awtkns commented 1 year ago

@daxxog thanks for this! Sorry for the delay. Would you be willing to add a test or two for this prior to merging?

daxxog commented 1 year ago

@awtkns Sure I can do that!

daxxog commented 1 year ago

@awtkns Is this along the lines of what you are looking for? https://github.com/awtkns/fastapi-crudrouter/pull/166/commits/8000bacadecb126a1149ff23f23073e248e96131

daxxog commented 1 year ago

@awtkns Is this along the lines of what you are looking for? 8000bac

@awtkns Disregard my previous comment. I ran coverage and realized my diff to the tests wasn't even covering my feature. I got the tests to actually run against my feature, and then discovered a few issues, which I fixed.