Get database items
Get a single item using an identifier
Section titled “Get a single item using an identifier”Every object in the database has a so-called object id, comparable to the primary key in relational databases. You can access this identifier like this:
object_id = myObject.db_id # myObject needs to be an instance of a class which inherits from DatabaseItem
# the object_id above now holds an instance of the class bson.ObjectId, but you usually want to have it as a string:object_id_str = str(myObject.db_id)You can use this unique identifier to fetch a single object by using the method DatabaseManager().get_item() and passing
the class to fetch as well as the object id:
from perseus.datamanager import DatabaseManager, Project
object_id = "67af092f16affd02f502c903a3"
db_manager = DatabaseManager()project = db_manager.get_item(Project, oid=object_id)if project is not None: ...If there are multiple possible results when calling DatabaseManager().get_item(), PERSEUS will return the first one it
fetched from the database. This is not always the first item that has been inserted!
Get a list of items
Section titled “Get a list of items”The following example returns a list of all available PERSEUS persons by using the method DatabaseManager().get_items().
from perseus.datamanager import DatabaseManager, Person
db_manager = DatabaseManager()person_list = db_manager.get_items(Person)if person_list is not None: for person in person_list: ...Search filter
Section titled “Search filter”You have the possibility to filter the results (works on both DatabaseManager().get_item() and DatabaseManager().get_items()).
To do so, pass a dictionary to the parameter search_filter, where each key has to be the name of the attribute which
you want to search for. Pass you search as value to that key. Only exact matches will be returned.
In this example, we want to fetch all projects which are of type “large”:
from perseus.datamanager import DatabaseManager, Project
project_list = DatabaseManager.get_items(Project, search_filter={"project_type": "large"})if project_list is not None: for project in project_list: ...