fairdm.core.sample.managers#

QuerySet for samples, with helpers that load related records and traverse sample hierarchies.

Module Contents#

Classes#

SampleQuerySet

QuerySet for samples, whose methods chain with the standard queryset operations.

API#

class SampleQuerySet(*args: Any, **kwargs: Any)#

Bases: fairdm.utils.location.managers.LocatedQuerySet, fairdm.core.managers.RecordVisibilityMixin, polymorphic.managers.PolymorphicQuerySet

QuerySet for samples, whose methods chain with the standard queryset operations.

inside(), within_distance() and nearest() find samples by their location.

Initialization

Load each sample’s dataset and project, and prefetch its contributors and roles.

Returns: The queryset with the related records loaded.

Example: python     samples = Sample.objects.with_related().filter(dataset=my_dataset)    

with_keywords()#

Prefetch the controlled keywords (the keywords many-to-many).

Split from :meth:with_metadata because the keywords are vocabulary concepts a sample is tagged with, not a per-sample record of its own.

Returns: The queryset with the keywords prefetched.

with_metadata()#

Prefetch the descriptions, dates and identifiers.

Returns: The queryset with the prefetches applied.

by_relationship(related_to=None, relationship_type=None)#

Filter samples by their relationship to another sample.

With related_to, returns the sources of relations targeting that sample, which are its children. Without it, returns every sample involved in a matching relation.

Args: related_to: The sample whose children are wanted. relationship_type: Only consider relations of this type, such as "child_of".

Returns: The samples matching the relationship criteria.

get_descendants(sample, max_depth=None)#

Return the descendants of a sample in the hierarchy.

The single traversal implementation for descendants, which Sample.get_descendants() delegates to. A SampleRelation has source as the child and target as the parent, so the walk goes from each level’s target to its source. It is an iterative breadth-first search, which suits moderate depths.

Args: sample: The sample to find descendants of. max_depth: The maximum depth to traverse. None is unlimited.

Returns: A queryset of the descendant samples.

get_ancestors(sample, max_depth=None)#

Return the ancestors of a sample in the hierarchy.

The single traversal implementation for ancestors, which Sample.get_ancestors() delegates to. It walks the reverse direction of get_descendants(), from each level’s source to its target, as an iterative breadth-first search.

Args: sample: The sample to find ancestors of. max_depth: The maximum depth to traverse. None is unlimited.

Returns: A queryset of the ancestor samples.