Added support to create Generic models #987
Open
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.
Description
Removed getitem from ModelMeta and replaced it with _class_getitem in Model class, which is described in PEP 560.
Also added to the function one condition, which checks if the key passed in is a tuple with classes or a single class, if it is, then it returns a _GenericAlias, which allows python to create that model as a Generic class instance.
If the user passes a key, which is not a class, it behaves the same way as before.
Motivation and Context
I was trying to make generic Models, which would allow me to create some generic methods, that would allow me to use them in all of my models. One example of such method would be:
It is a helper method, which returns a view class, that is later returned to the user, thanks to the Generic ability of that model, i need only to reimplement the abstract to_view method, and all the other helper methods have already the required type.
How Has This Been Tested?
Obviously i tested it only in my own code, so i couldn't cover all scenarios, but i also ran all tests from "make test", and "test_sqlite" without any errors.
In my understanding, it shouldn't affect any other areas, because the method before was only to get one model instance by using the models class as an array, accesing it by it's primary key. Hopefully i'm right, if not, then sorry in advance :(
Checklist: