Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

daxxog
Copy link

@daxxog daxxog commented Aug 26, 2022

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
Copy link

vercel bot commented Aug 26, 2022

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
Copy link
Owner

awtkns commented Sep 14, 2022

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

@daxxog
Copy link
Author

daxxog commented Sep 15, 2022

@awtkns Sure I can do that!

@daxxog
Copy link
Author

daxxog commented Sep 17, 2022

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

@daxxog
Copy link
Author

daxxog commented Sep 17, 2022

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants