Skip to content

Add fk_as_int option to get_pydantic()#893

Open
iron3oxide wants to merge 8 commits intoormar-orm:masterfrom
iron3oxide:master
Open

Add fk_as_int option to get_pydantic()#893
iron3oxide wants to merge 8 commits intoormar-orm:masterfrom
iron3oxide:master

Conversation

@iron3oxide
Copy link

This allows to create pydantic models that contain the FK ID instead of the full related model for each field specified in fk_as_int. To see why this can be useful, check #343.

Unfortunately I didn't manage to allow for more than one level of nesting in the given dict/set, meaning one can only skip the conversion of a directly related model, not an indirectly related one. A more concrete example of this caveat has been added to the docs.

jh and others added 7 commits October 23, 2022 02:08
…nt in the resulting pydantic model

This can be useful when using get_pydantic() to create read or create models, as one probably doesn't want to have to specify the whole related model in a request in order to pass validation, see ormar-orm#343.
More specific testing would have required new models, which I frankly was too lazy to implement. Also fixed a typo in the file name.
This way "fk_as_int={"item__category"} should generate a nested item model in which the related category is not a nested model but an ID/PositiveInt. I tried to allow for infinite nesting in fk_as_int but didn't manage to implement it, in fact idk if it's even possible to do with the current relation algorithm.
This way there are two separate recursion paths, which might be the better solution.
Caveat: No M2M tests yet
Might also be useful in the fastapi parts of the documentation, but the methods section is prominently linked there.
@codecov-commenter
Copy link

codecov-commenter commented Apr 2, 2023

Codecov Report

Merging #893 (b36f055) into master (40c3b3d) will not change coverage.
The diff coverage is 100.00%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #893   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          201       201           
  Lines        16647     16680   +33     
=========================================
+ Hits         16647     16680   +33     
Files Coverage Δ
ormar/models/mixins/pydantic_mixin.py 100.00% <100.00%> (ø)
...ts/test_fastapi/test_excludes_with_get_pydantic.py 100.00% <100.00%> (ø)
...ydantic_generation/test_getting_pydantic_models.py 100.00% <100.00%> (ø)

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.

3 participants