[New feature]: Auto generate API model from SQLAlchemy model #493
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello.
This PR will allow developers to automate the creation of API models while keeping customization possible.
PS: There is already an open PR that attempts to solve the same problem #489
Current behaviour (minimal example)
Developers need to manually create API models like this example:
New feature (minimal example)
Customization
The helper function definition:
Args meaning:
api
: Your Flask RestX API instance objectmodel
: YourSQLAlchemy/Flask SQLALchemy
model objectforce_camel_case
: By default JSON schemas fields are written incamelCase
format and developers can switch this feature off by settingsforce_camel_case=False
ignore_attributes
: By default none of your model's attributes are ignored, but in need you can manually add themfields
: List of the model's fields to be mapped (See below for more customizations); by default all your model's attributes are mapped (another way to add/exclude model's attributes; seeignore_attributes
)Foreigneys
are mapped by default in their respective nested formats (even for multi level relations between database tables) only iffields
argument is empty otherwise onlyfields
will be mapped.More customization
Developers can add a
Meta
class in their models definitions in order to control which attributes should be in their finalJSON schemas
or adding them to theArg: fields
.Example:
Is equivalent to:
For more details and customizations, See the
tests/test_auto_gen_api_model.py
.New package added during tests:
Flask-SQLAlchemy==3.0.2
is added totest.pip
requirement fileBest regards.