MCPcopy
hub / github.com/ormar-orm/ormar / create_dummy_instance

Function create_dummy_instance

ormar/fields/foreign_key.py:22–49  ·  view source on GitHub ↗

Ormar never returns you a raw data. So if you have a related field that has a value populated it will construct you a Model instance out of it. Creates a "fake" instance of passed Model from pk value. The instantiated Model has only pk value filled. To achieve this __pk_onl

(fk: type["T"], pk: Any = None)

Source from the content-addressed store, hash-verified

20
21
22def create_dummy_instance(fk: type["T"], pk: Any = None) -> "T":
23 """
24 Ormar never returns you a raw data.
25 So if you have a related field that has a value populated
26 it will construct you a Model instance out of it.
27
28 Creates a "fake" instance of passed Model from pk value.
29 The instantiated Model has only pk value filled.
30 To achieve this __pk_only__ flag has to be passed as it skips the validation.
31
32 If the nested related Models are required they are set with -1 as pk value.
33
34 :param fk: class of the related Model to which instance should be constructed
35 :type fk: Model class
36 :param pk: value of the primary_key column
37 :type pk: Any
38 :return: Model instance populated with only pk
39 :rtype: Model
40 """
41 init_dict = {
42 **{fk.ormar_config.pkname: pk or -1},
43 **{
44 k: create_dummy_instance(v.to)
45 for k, v in fk.ormar_config.model_fields.items()
46 if v.is_relation and not v.nullable and not v.virtual
47 },
48 }
49 return cast("T", fk._internal_construct(_pk_only=True, _excluded=None, **init_dict))
50
51
52def create_dummy_model(

Callers 1

Calls 1

_internal_constructMethod · 0.80

Tested by

no test coverage detected