lamindb.core.QueryManager

class lamindb.core.QueryManager(*args, **kwargs)

Bases: Manager

Manage queries through fields.

Examples

>>> ln.save(ln.ULabel.from_values(["ULabel1", "ULabel2", "ULabel3"], field="name"))  # noqa
>>> labels = ln.ULabel.filter(name__icontains = "label").all()
>>> ln.ULabel(name="ULabel1").save()
>>> label = ln.ULabel.get(name="ULabel1")
>>> label.parents.set(labels)
>>> manager = label.parents
>>> manager.df()

Attributes

auto_created = False
creation_counter = 205
property db
use_in_migrations = False

If set to True the manager will be serialized into migrations and will thus be available in e.g. RunPython operations.

Class methods

classmethod from_queryset(queryset_class, class_name=None)

Methods

aaggregate(*args, **kwargs)
abulk_create(objs, batch_size=None, ignore_conflicts=False, update_conflicts=False, update_fields=None, unique_fields=None)
abulk_update(objs, fields, batch_size=None)
acontains(obj)
acount()
acreate(**kwargs)
aearliest(*fields)
aexists()
aexplain(*, format=None, **options)
afirst()
aget(*args, **kwargs)
aget_or_create(defaults=None, **kwargs)
aggregate(*args, **kwargs)

Return a dictionary containing the calculations (aggregation) over the current queryset.

If args is present the expression is passed as a kwarg using the Aggregate object’s default alias.

ain_bulk(id_list=None, *, field_name='pk')
aiterator(chunk_size=2000)

An asynchronous iterator over the results from applying this QuerySet to the database.

alast()
alatest(*fields)
alias(*args, **kwargs)

Return a query set with added aliases for extra data or aggregations.

all()

Return QuerySet of all.

For **kwargs, see lamindb.core.QuerySet.df().

annotate(*args, **kwargs)

Return a query set in which the returned objects have been annotated with extra data or aggregations.

aupdate(**kwargs)
aupdate_or_create(defaults=None, create_defaults=None, **kwargs)
bulk_create(objs, batch_size=None, ignore_conflicts=False, update_conflicts=False, update_fields=None, unique_fields=None)

Insert each of the instances into the database. Do not call save() on each of the instances, do not send any pre/post_save signals, and do not set the primary key attribute if it is an autoincrement field (except if features.can_return_rows_from_bulk_insert=True). Multi-table models are not supported.

bulk_update(objs, fields, batch_size=None)

Update the given fields in each of the given objects in the database.

check(**kwargs)
complex_filter(filter_obj)

Return a new QuerySet instance with filter_obj added to the filters.

filter_obj can be a Q object or a dictionary of keyword lookup arguments.

This exists to support framework features such as ‘limit_choices_to’, and usually it will be more natural to use other methods.

contains(obj)

Return True if the QuerySet contains the provided obj, False otherwise.

contribute_to_class(cls, name)
count()

Perform a SELECT COUNT() and return the number of records as an integer.

If the QuerySet is already fully cached, return the length of the cached results set to avoid multiple SELECT COUNT(*) calls.

create(**kwargs)

Create a new object with the given kwargs, saving it to the database and returning the created object.

dates(field_name, kind, order='ASC')

Return a list of date objects representing all available dates for the given field_name, scoped to ‘kind’.

datetimes(field_name, kind, order='ASC', tzinfo=None)

Return a list of datetime objects representing all available datetimes for the given field_name, scoped to ‘kind’.

db_manager(using=None, hints=None)
deconstruct()

Return a 5-tuple of the form (as_manager (True), manager_class, queryset_class, args, kwargs).

Raise a ValueError if the manager is dynamically generated.

defer(*fields)

Defer the loading of data for certain fields until they are accessed. Add the set of deferred fields to any existing set of deferred fields. The only exception to this is if None is passed in as the only parameter, in which case removal all deferrals.

df(**kwargs)

Convert to DataFrame.

For **kwargs, see lamindb.core.QuerySet.df().

difference(*other_qs)
distinct(*field_names)

Return a new QuerySet instance that will select only distinct results.

earliest(*fields)
exclude(*args, **kwargs)

Return a new QuerySet instance with NOT (args) ANDed to the existing set.

exists()

Return True if the QuerySet would have any results, False otherwise.

explain(*, format=None, **options)

Runs an EXPLAIN on the SQL query this QuerySet would perform, and returns the results.

extra(select=None, where=None, params=None, tables=None, order_by=None, select_params=None)

Add extra SQL fragments to the query.

filter(*args, **kwargs)

Return a new QuerySet instance with the args ANDed to the existing set.

first()

Return the first object of a query or None if no match is found.

get(*args, **kwargs)

Perform the query and return a single object matching the given keyword arguments.

get_or_create(defaults=None, **kwargs)

Look up an object with the given kwargs, creating one if necessary. Return a tuple of (object, created), where created is a boolean specifying whether an object was created.

get_queryset()

Return a new QuerySet object. Subclasses can override this method to customize the behavior of the Manager.

in_bulk(id_list=None, *, field_name='pk')

Return a dictionary mapping each of the given IDs to the object with that ID. If id_list isn’t provided, evaluate the entire QuerySet.

intersection(*other_qs)
iterator(chunk_size=None)

An iterator over the results from applying this QuerySet to the database. chunk_size must be provided for QuerySets that prefetch related objects. Otherwise, a default chunk_size of 2000 is supplied.

last()

Return the last object of a query or None if no match is found.

latest(*fields)

Return the latest object according to fields (if given) or by the model’s Meta.get_latest_by.

list(field=None)

Populate a list with the results.

Examples

>>> ln.save(ln.ULabel.from_values(["ULabel1", "ULabel2", "ULabel3"], field="name"))
>>> labels = ln.ULabel.filter(name__icontains="label").all()
>>> ln.ULabel(name="ULabel1").save()
>>> label = ln.ULabel.get(name="ULabel1")
>>> label.parents.set(labels)
>>> label.parents.list()
>>> label.parents.list("name")
['ULabel1', 'ULabel2', 'ULabel3']
lookup(field=None, **kwargs)

Return an auto-complete object for a field.

Parameters:
  • field (str | DeferredAttribute | None, default: None) – The field to look up the values for. Defaults to first string field.

  • return_field – The field to return. If None, returns the whole record.

Return type:

NamedTuple

Returns:

A NamedTuple of lookup information of the field values with a dictionary converter.

See also

search()

Examples

>>> import bionty as bt
>>> bt.settings.organism = "human"
>>> bt.Gene.from_source(symbol="ADGB-DT").save()
>>> lookup = bt.Gene.lookup()
>>> lookup.adgb_dt
>>> lookup_dict = lookup.dict()
>>> lookup_dict['ADGB-DT']
>>> lookup_by_ensembl_id = bt.Gene.lookup(field="ensembl_gene_id")
>>> genes.ensg00000002745
>>> lookup_return_symbols = bt.Gene.lookup(field="ensembl_gene_id", return_field="symbol")
none()

Return an empty QuerySet.

only(*fields)

Essentially, the opposite of defer(). Only the fields passed into this method and that are not already specified as deferred are loaded immediately when the queryset is evaluated.

order_by(*field_names)

Return a new QuerySet instance with the ordering changed.

Return a new QuerySet instance that will prefetch the specified Many-To-One and Many-To-Many related objects when the QuerySet is evaluated.

When prefetch_related() is called more than once, append to the list of prefetch lookups. If prefetch_related(None) is called, clear the list.

raw(raw_query, params=(), translations=None, using=None)
reverse()

Reverse the ordering of the QuerySet.

search(string, **kwargs)

Search.

Parameters:
  • string (str) – The input string to match against the field ontology values.

  • field – The field or fields to search. Search all string fields by default.

  • limit – Maximum amount of top results to return.

  • case_sensitive – Whether the match is case sensitive.

Returns:

A sorted DataFrame of search results with a score in column score. If return_queryset is True. QuerySet.

See also

filter() lookup()

Examples

>>> ulabels = ln.ULabel.from_values(["ULabel1", "ULabel2", "ULabel3"], field="name")
>>> ln.save(ulabels)
>>> ln.ULabel.search("ULabel2")
select_for_update(nowait=False, skip_locked=False, of=(), no_key=False)

Return a new QuerySet instance that will select objects with a FOR UPDATE lock.

Return a new QuerySet instance that will select related objects.

If fields are specified, they must be ForeignKey fields and only those related objects are included in the selection.

If select_related(None) is called, clear the list.

union(*other_qs, all=False)
update(**kwargs)

Update all elements in the current QuerySet, setting all the given fields to the appropriate values.

update_or_create(defaults=None, create_defaults=None, **kwargs)

Look up an object with the given kwargs, updating one with defaults if it exists, otherwise create a new one. Optionally, an object can be created with different values than defaults by using create_defaults. Return a tuple (object, created), where created is a boolean specifying whether an object was created.

using(alias)

Select which database this QuerySet should execute against.

values(*fields, **expressions)
values_list(*fields, flat=False, named=False)