Embedding Models in Edgy¶
Edgy provides two primary methods for embedding models within other models, offering flexibility and control over how data is structured and accessed.
1. Embeddable Models¶
Edgy models can be seamlessly used as embeddables, treating them like fields within another model. This approach automatically copies the fields of the embedded model, prefixing them with the attribute name used for embedding, followed by an underscore (_
).
Fields with inherit=False
are excluded from the embedding process. This prevents fields like PKField
or auto-injected id
fields from being unintentionally included.
from typing import ClassVar
import edgy
class InheritableModel(edgy.Model):
first_name: str = edgy.CharField(max_length=255)
last_name: str = edgy.CharField(max_length=255)
class Meta:
abstract = True
class NonInheritableModel(edgy.Model):
age: int = edgy.IntegerField()
class Meta:
abstract = True
inherit = False
class MyModel(edgy.Model):
model1: ClassVar[InheritableModel] = InheritableModel
# ClassVar is optional
model2 = NonInheritableModel
class AnotherModel(MyModel):
pass
# NonInheritableModel is gone
Explanation:
- InheritableModel: This abstract model defines
first_name
andlast_name
fields. WhenInheritableModel
is used as an embeddable inMyModel
, these fields will be copied and prefixed. - NonInheritableModel: This abstract model defines an
age
field and setsinherit=False
. This ensures that whenNonInheritableModel
is used as an embeddable, its fields are not inherited by submodels. - MyModel: This model embeds
InheritableModel
asmodel1
andNonInheritableModel
asmodel2
. - AnotherModel: This model inherits from
MyModel
. Due toinherit=False
inNonInheritableModel
, themodel2
fields are not included inAnotherModel
.
Key Benefits:
- Simplified model composition: Embeddables allow you to structure complex data models by combining simpler, reusable models.
- Automatic field prefixing: Prefixes prevent naming conflicts when multiple embeddables are used in a model.
- Control over inheritance: The
inherit
parameter provides fine-grained control over which fields are inherited by submodels.
2. Explicit Control with CompositeField¶
For more explicit control over the embedding process, you can use the CompositeField
and pass models as its inner_fields
argument. This method provides greater flexibility in defining how embedded models are handled.
from typing import ClassVar
import edgy
class InheritableModel(edgy.Model):
first_name: str = edgy.CharField(max_length=255)
last_name: str = edgy.CharField(max_length=255)
class Meta:
abstract = True
class NonInheritableModel(edgy.Model):
age: int = edgy.IntegerField()
class Meta:
abstract = True
inherit = False
class MyModel(edgy.Model):
model1: InheritableModel = edgy.CompositeField(inner_fields=InheritableModel)
# note: the inherit information can be overwritten
model2 = edgy.CompositeField(inner_fields=NonInheritableModel, inherit=True)
class AnotherModel(MyModel):
pass
# NonInheritableModel is gone
Explanation:
- CompositeField: This field allows you to explicitly define how embedded models are handled.
- inner_fields: This parameter specifies the model(s) to be embedded.
- inherit: The
inherit
parameter can be used to override theinherit
setting of the embedded model.
Key Benefits:
- Explicit control:
CompositeField
provides explicit control over which fields are embedded and how they are handled. - Flexibility: You can override the
inherit
setting of embedded models, providing greater flexibility in model composition. - Allows the use of other CompositeField parameters.
When to Use Each Method:
- Use embeddable models when you want a simple and automatic way to embed models within other models.
- Use
CompositeField
when you need more explicit control over the embedding process, such as overriding inheritance settings or using other CompositeField settings.
In summary:
Edgy's embedding capabilities allow you to create complex and well-structured data models by combining simpler, reusable models. Whether you choose the automatic embeddable approach or the explicit CompositeField
method, Edgy provides the tools you need to effectively manage your data.