Replies: 1 comment 2 replies
-
I observed for a while how people were just using indeed you have to "register" it somewhere before you import your model-schemas, but overall that looks easy the problem with |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
With the merging of #1352, a long standing feature request for support for custom field types has now been solved. However, after thinking about it for a bit, I think this solution has a few problems.
The global list for type mapping was simple, but I think that's fine. However, having a function that simply edits that list is a "code smell", I know Django relies on globals a lot but this feels like a step too far, and it has some knock on effects.
Because a function must be called to register a type, that function now must be called before the schema using it is initialized. I suppose since models already must be loaded first this might always be OK, but then the registration call has to live somewhere that gets loaded first. This is really a piece of configuration, so maybe it should live in settings.py? But it's a function call, not a list or dictionary...
I think this is the biggest thing the current solution lacks. There are a number of libraries that implement custom fields that are just subclassing built-in Django fields. The current solution requires you to register each of these manually, but it would be a lot nicer if Ninja could determine this automatically. For fully custom fields that also have subclasses, these all must be manually registered too.
Additionally, because it's a function call, library authors who may wish to add support for Ninja to their library have a more difficult task. They cannot simply import that function and call it, they need to check if Ninja is an available library and they probably should also check if it's being loaded in the Django project.
With that all in mind, I think this solution #1320 had some advantages in this regard. It avoids global variables and load order issues, and provides an avenue for inheritance and library support. I would like to see some kind of hybrid solution, one that can either pull the type from the special function on the field, or introspect its class and pull a type from the mapping of built-in fields.
Beta Was this translation helpful? Give feedback.
All reactions