pydent.relationships.HasOneFromMany¶
-
class
pydent.relationships.
HasOneFromMany
(nested, ref_model=None, attr=None, ref=None, additional_args=None, callback=None, callback_kwargs=None, **kwargs)[source]¶ Bases:
pydent.relationships.One
Returns a single model from a Many relationship.
HasOneFromMany relationship initializer, which is intended to return a single model from a Many query.
- Parameters
nested (str) – Model class name for this relationship
ref_model – Reference model name of the model owning this relationships.
@add_schema class Author(ModelBase): # search for books using 'author_id' fields=dict(books=HasMany("Book", "Author"))
- Parameters
attr (str) – Attribute name to use with reference model (default=’id’). For example “Author” => ‘author_id’
ref (str) – The reference to use to find models. If none, a reference is built from the ‘ref_model’ and ‘attr’ parameters
-
__init__
(nested, ref_model=None, attr=None, ref=None, additional_args=None, callback=None, callback_kwargs=None, **kwargs)[source]¶ HasOneFromMany relationship initializer, which is intended to return a single model from a Many query.
- Parameters
nested (str) – Model class name for this relationship
ref_model – Reference model name of the model owning this relationships.
@add_schema class Author(ModelBase): # search for books using 'author_id' fields=dict(books=HasMany("Book", "Author"))
- Parameters
attr (str) – Attribute name to use with reference model (default=’id’). For example “Author” => ‘author_id’
ref (str) – The reference to use to find models. If none, a reference is built from the ‘ref_model’ and ‘attr’ parameters
Methods
__init__
(nested[, ref_model, attr, ref, …])HasOneFromMany relationship initializer, which is intended to return a single model from a Many query.
build_query
(models)Bundles all of the callback args for the models into a single query.
cache_result
(owner, val)deserialize
(owner, val)fullfill
(owner[, cache, extra_args, …])Calls the callback function using the owner object.
get_callback_args
(owner[, extra_args])Processes the callback args.
get_callback_kwargs
(owner, extra_kwargs)Processes the callback kwargs.
register
(name, objtype)Registers the field to a nested class.
serialize
(owner, obj)set_data_key
(key)Attributes
QUERY_TYPE
SELF
-
ACCESSOR
¶ alias of
BaseRelationshipAccessor
-
_Callback__FLAGS
¶ alias of
pydent.marshaller.fields.Callback.__FLAGS
-
_get_ref_attr
(nested=None, ref=None, attr=None)¶ Sets the ‘ref’ and ‘attr’ attributes. These attributes are used to defined parameters for.
pydent.marshaller.Relation
classes.For example:
relation # HasOne, HasMany, or HasManyGeneric, etc. relation.set_ref(ref="parent_id") relation.ref # "parent_id" relation.attr # "id" relation.set_ref(model="SampleType") relation.ref # "sample_type_id" relation.attr # "id" relation.set_ref(attr="name", model="OperationType") relation.ref # "operation_type_name relation.attr # "name"
-
build_query
(models)¶ Bundles all of the callback args for the models into a single query.
-
fullfill
(owner, cache=None, extra_args=None, extra_kwargs=None)¶ Calls the callback function using the owner object. A Callback.SELF arg value will be replaced to be equivalent to the owner instance model.
- Parameters
owner – the owning object
cache – if True, will cache the return in the deserialized data. On next call, the cached result will be returned.
extra_args – extra args to pass to the callback function
extra_kwargs – extra kwargs to pass to the callback function
- Returns
function result
- Return type
any
-
get_callback_args
(owner, extra_args=None)¶ Processes the callback args.
- Return type
List
[Any
]
-
get_callback_kwargs
(owner, extra_kwargs)¶ Processes the callback kwargs.
- Return type
dict
-
register
(name, objtype)¶ Registers the field to a nested class. Instantiates the corresponding descriptor (i.e. accessor)
- Parameters
name (
str
) – name of the fieldobjtype (
Type
) – the nested class to register the field to
- Returns
None
- Return type
None