Context#
relationalai
#class Context(dynamic: bool = False)
Contexts execute blocks of code written in RelationalAI’s declarative query builder syntax.
You create contexts using Model
methods,
such as Model.query()
and Model.rule()
.
Parameters#
Name | Type | Description |
---|---|---|
dynamic | bool | Whether or not the context is dynamic. Dynamic contexts support Python control flow as query builder macros. |
Attributes#
Name | Description | Type |
---|---|---|
model | The model for which the context is created. | Model |
results | The results of a query context. | Varies |
Methods#
Name | Description | Returns |
---|---|---|
.__enter__() | Enters the context. Implements the context manager protocol. | Context |
.__exit__() | Exits the context. Implements the context manager protocol. | None |
Example#
You create Context
objects using Model
methods,
such as Model.rule()
and Model.query()
.
Context
objects are context managers
and must be called in a with
statement:
#import relationalai as rai
model = rai.Model("myModel")
MyType = model.Type("MyType")
# Create a rule context with `model.rule()` that adds an object to `MyType`.
with model.rule():
MyType.add(name="my first object")
# Create a query context with `model.query()` to query the model.
with model.query() as select:
obj = MyType()
response = select(obj.name)
print(response.results)
# Output:
# name
# 0 my first object
The following Model
methods all return Context
objects: