Option sets¶
client.option_sets — CRUD + bulk-sync over /api/optionSets. An OptionSet is a controlled-vocabulary list of Option values (e.g. "Yes / No / Unknown"); referenced by DataElement.optionSet and TrackedEntityAttribute.optionSet to constrain captured values.
async with Dhis2Client(...) as client:
# Declarative spec — name + options, server fills in the rest.
report = await client.option_sets.sync(
name="Yes/No/Unknown",
options=[
OptionSpec(code="Y", name="Yes"),
OptionSpec(code="N", name="No"),
OptionSpec(code="U", name="Unknown"),
],
)
# `report` is an `UpsertReport` listing which options were created / updated / removed.
The sync() helper diffs the desired option list against the live set and issues exactly the writes needed — useful for CI-driven option-list management. Individual list_all / get / create / update / delete verbs follow the standard accessor pattern.
Worked example: examples/v42/client/options_integration.py.
option_sets
¶
Integration-grade helpers for DHIS2 OptionSets + Options.
Generic CRUD for option sets + individual options is covered by
client.resources.option_sets and client.resources.options (generated).
This accessor adds the workflow primitives external integrations
typically reach for:
- Resolve an OptionSet by its business code (external systems tend to know "VACCINE_TYPE", not the DHIS2 UID).
- Walk a set's options in sort order without the array-of-references hoop.
- Pinpoint a single option by code or display name without pulling the whole set into memory.
- Idempotent bulk sync — given a list of
(code, name)pairs, add the new ones, update names on existing codes, optionally remove options missing from the spec. The canonical ETL pattern for keeping a DHIS2 controlled vocabulary in step with an external source of truth.
DHIS2 stores sortOrder as a 0-indexed integer matching array
position; the upsert helper writes it the same way, so round-tripping
through the server stays stable.
Classes¶
OptionSpec
¶
Bases: BaseModel
One option in a upsert_options spec — identified by business code.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/option_sets.py
UpsertReport
¶
Bases: BaseModel
Summary of an upsert_options run — codes grouped by the action taken.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/option_sets.py
OptionSetsAccessor
¶
Dhis2Client.option_sets — integration helpers over /api/optionSets + /api/options.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/option_sets.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
Functions¶
__init__(client)
¶
Bind to the sharing client — reuses its auth + HTTP pool for every request.
list_all(*, page=1, page_size=50, include_options=False)
async
¶
Page through OptionSets. include_options=True resolves each set's options inline.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/option_sets.py
get_by_code(code, *, include_options=True)
async
¶
Fetch an OptionSet by its business code; return None if no match.
External integrations routinely know a set by a stable code (e.g.
VACCINE_TYPE) rather than the DHIS2 UID. include_options=True
(default) pulls every option inline (id + code + name + sortOrder).
Set False when you only need metadata for an existence check.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/option_sets.py
list_options(option_set_uid)
async
¶
Return every option in a set, ordered by sortOrder (ascending).
Streams /api/options?filter=optionSet.id:eq:{uid} with paging off,
so a set with 10,000 options still returns in one call. DHIS2
enforces a practical ceiling on option-set size — if you're
hitting it, use the generated accessor with explicit paging.
Fields include attributeValues so upsert_options can preserve
them when updating an option's name or sort order.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/option_sets.py
find_option(option_set_uid, *, option_code=None, option_name=None)
async
¶
Locate one option in a set by code or display name; None if not found.
Exactly one of option_code / option_name must be provided.
Filters run server-side — cheap for big sets. option_code
matches via :eq: (exact); option_name uses :eq: too since
display names are already unique within a set.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/option_sets.py
upsert_options(option_set_uid, spec, *, remove_missing=False, dry_run=False)
async
¶
Reconcile the set's options against spec; return the diff as a typed report.
For each entry in spec:
- Code not in the current set → ADD (mints a UID, creates).
- Code in the current set but name or sort order differs → UPDATE.
- Code in the current set and fully matches → SKIP (no-op).
If remove_missing=True, any current option whose code isn't in
spec is REMOVED. Defaults off — the safer posture for
ETL pipelines where the spec is a partial refresh rather than a
full-catalogue replacement.
sort_order on each OptionSpec is optional; missing values are
filled in with the spec-list index (0-based, matching DHIS2's
internal convention). Callers driving a reorder can pin explicit
values.
dry_run=True computes the report without writing anything.
Useful for previewing the effect in CI pipelines.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/option_sets.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
get_option_attribute_value(option_uid, attribute_code_or_uid)
async
¶
Read one attribute value off an Option; None if the attribute isn't set.
Source code in packages/dhis2w-client/src/dhis2w_client/v42/option_sets.py
set_option_attribute_value(option_uid, attribute_code_or_uid, value)
async
¶
Set / replace one attribute value on an Option (read-merge-write).
Source code in packages/dhis2w-client/src/dhis2w_client/v42/option_sets.py
find_option_by_attribute(option_set_uid, attribute_code_or_uid, value)
async
¶
Reverse lookup — find the Option in a set whose attribute matches a value.
Thin wrapper over client.attribute_values.find_one_uid_by_value(...)
that scopes the query to one OptionSet (extra_filters narrows the
search) and returns a typed Option model. On miss returns None.