Created
November 7, 2023 10:03
Bravelab__Blog__Hybrid_Model_#2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HybridModelMeta(models.base.ModelBase): | |
""" | |
Metaclass which is responsible for creating descriptor fields in the model | |
based on fields of Mongodb document. | |
""" | |
def __new__(cls, name, bases, attrs, **kwargs): | |
new_attrs = {} | |
if document_cls := attrs.get("_document_cls"): | |
for doc_field, doc_field_val in document_cls._fields.items(): | |
if not doc_field_val.primary_key: | |
new_attrs[doc_field] = MongoFieldDescriptor() | |
attrs.update(new_attrs) | |
return super().__new__(cls, name, bases, attrs, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment