Add support for custom django fields (#1109) #1320
Open
+44
−3
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.
Motivation
Using exclusively
django.db.fields.Field.get_internal_type
to determine which Pydantic field to use in the built schemas makes it difficult to create custom django fields. This PR adds support for a new methodget_schema_type
, which allows usingget_internal_type
for its intended purpose (determining database column types, etc.), while at the same time allowing specifying which Pydantic schema field to use.Concerns
get_schema_type
function in their field classes. Right now there is no checks or fallbacks for this scenario.get_schema_type
is somewhat generic. I have no better ideas though.Monkey-patchingThis is no longer relevant.ninja.orm.fields.TYPES
is not a great solution for future-proofing the solutions using the provided pattern to define custom fields. The should probably be an additional function inninja.orm.fields
to do this.Notes
I don't currently have time to test the code, but see no reason why it shouldn't work. If you want to wait, I can write proper tests in a couple of weeks.