Skip to content

Instantly share code, notes, and snippets.

@msmenzyk
Created November 7, 2023 10:03
Bravelab__Blog__Hybrid_Model_#2
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