Marshall class¶
edgy.core.marshalls.Marshall
¶
Marshall(instance=None, **kwargs)
Bases: BaseMarshall
Concrete implementation of a Marshall, requiring the __model__ attribute
to be set in its Config or Meta class.
Initializes a BaseMarshall instance.
| PARAMETER | DESCRIPTION |
|---|---|
instance
|
An optional Edgy model instance to populate the marshall.
TYPE:
|
**kwargs
|
Arbitrary keyword arguments to initialize marshall fields.
TYPE:
|
Source code in edgy/core/marshalls/base.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
instance
property
writable
¶
instance
Returns the associated Edgy model instance.
If the instance is not yet set or resolved (due to lazy initialization),
it calls _setup() to create and resolve it.
has_instance
property
¶
has_instance
Checks if an Edgy model instance is currently associated with this marshall.
valid_fields
cached
property
¶
valid_fields
Returns a dictionary of all Pydantic fields in the marshall that are not marked for exclusion. This property is cached.
fields
cached
property
¶
fields
Returns a dictionary of all custom BaseMarshallField instances defined
directly on the marshall. This property is cached.
model_dump
¶
model_dump(show_pk=None, **kwargs)
An updated and enhanced version of the Pydantic model_dump method.
This method provides fine-grained control over how the model's data is serialized into a dictionary. It specifically addresses: - Enforcing the inclusion of primary key fields. - Correctly handling fields marked for exclusion. - Applying custom logic for fields that retrieve their values via getters or require special serialization (e.g., related models, composite fields).
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the Pydantic
TYPE:
|
show_pk
|
An optional boolean flag. If
TYPE:
|
**kwargs
|
Arbitrary keyword arguments that are passed directly to the
underlying Pydantic
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
A |
dict[str, Any]
|
inclusions, exclusions, and special field handling. |
Source code in edgy/core/db/models/mixins/dump.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
model_dump_json
¶
model_dump_json(**kwargs)
Dumps the model data into a JSON string.
This method leverages model_dump with mode="json" and then uses orjson
for efficient JSON serialization, which is faster than Python's built-in json module.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the
TYPE:
|
**kwargs
|
Arbitrary keyword arguments passed to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
A |
Source code in edgy/core/db/models/mixins/dump.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
_setup
¶
_setup()
Assembles a new Edgy model instance based on the marshall's current field values.
This method is called when self.instance is accessed for the first time
and no instance was provided during initialization.
| RETURNS | DESCRIPTION |
|---|---|
Model
|
The assembled Edgy model instance.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If the marshall is declared with |
Source code in edgy/core/marshalls/base.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
_resolve_async
async
¶
_resolve_async(name, awaitable)
Asynchronously resolves an awaitable and sets the result to a marshall field.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name of the marshall field to set.
TYPE:
|
awaitable
|
The awaitable object to resolve.
TYPE:
|
Source code in edgy/core/marshalls/base.py
187 188 189 190 191 192 193 194 195 | |
_resolve_serializer
¶
_resolve_serializer(instance)
Resolves the marshall's custom fields by populating them with data from the provided Edgy model instance. Handles both direct attribute access and method-based field resolution, including asynchronous getters.
| PARAMETER | DESCRIPTION |
|---|---|
instance
|
The Edgy model instance to extract data from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BaseMarshall
|
The marshall instance with its fields populated.
TYPE:
|
Source code in edgy/core/marshalls/base.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
_get_method_value
¶
_get_method_value(name, instance)
Retrieves the value for a method-based marshall field. It expects a
method named get_<field_name> on the marshall instance.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name of the method-based field (e.g., 'full_name' for
TYPE:
|
instance
|
The Edgy model instance to pass to the getter method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
The value returned by the getter method.
TYPE:
|
Source code in edgy/core/marshalls/base.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
_handle_primary_key
¶
_handle_primary_key(instance)
Synchronizes the primary key fields of the marshall with those of the provided model instance after a save operation. This is crucial for newly created instances where the PK might be generated by the database.
| PARAMETER | DESCRIPTION |
|---|---|
instance
|
The Edgy model instance from which to copy primary key values.
TYPE:
|
Source code in edgy/core/marshalls/base.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
save
async
¶
save()
Calls the save method of the BaseMarshall, ensuring type hinting for Self.
Source code in edgy/core/marshalls/base.py
353 354 355 356 357 | |