Skip to content

OnyxField

Class that represents a single field-value pair for use in Onyx queries.

__init__(**kwargs)

Initialise a field.

PARAMETER DESCRIPTION
**kwargs

Keyword arguments containing a single key-value pair.

TYPE: Any DEFAULT: {}

Notes
  • Takes a single key-value argument as input.
  • The key corresponds to a field (and optional lookup) to use for filtering.
  • The value corresponds to the field value(s) that are being matched against.
  • OnyxField instances can be combined into complex expressions using Python's bitwise operators: & (AND), | (OR), ^ (XOR), and ~ (NOT).
  • Multi-value lookups (e.g. in, range) support passing a Python list as the value. These are coerced into comma-separated strings internally.

Examples:

Create OnyxField objects and combine them using Python bitwise operators:

from onyx import OnyxField

field1 = OnyxField(field1="value1")
field2 = OnyxField(field2__contains="value2")

expression = (field1 | field2) & OnyxField(
    published_date__range=["2023-01-01", "2023-01-02"]
)
>>> field1
<onyx.field.OnyxField object at 0x1028eb850>
>>> field2
<onyx.field.OnyxField object at 0x1028eb850>
>>> expression
<onyx.field.OnyxField object at 0x103b6fc40>
>>> field1.query
{"field1": "value1"}
>>> field2.query
{"field2__contains": "value2"}
>>> expression.query
{
    "&": [
        {"|": [{"field1": "value1"}, {"field2__contains": "value2"}]},
        {"published_date__range": "2023-01-01,2023-01-02"},
    ]
}