Skip to content

Commit 0098247

Browse files
authored
Quick reference for working with entities in forms (#1925)
1 parent 013c235 commit 0098247

File tree

5 files changed

+272
-2
lines changed

5 files changed

+272
-2
lines changed

docs/entities-intro.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Introduction to Entities
66

77
If you've heard something about ODK Entities and want to better understand whether they're useful for your longitudinal data collection, you're in the right place! We've organized this page as a series of questions that are independent from each other so you can focus on the topics that interest you.
88

9-
If you're someone who learns best by doing, you may prefer to jump straight into the tutorial on :doc:`building a community reporting tool with Entities <tutorial-community-reporting>` and to come back here if you have any questions. If you have a question that we haven't answered, you can `post on the forum <https://forum.getodk.org/c/support/6>`_.
9+
For a quick reference, check out the :doc:`Entity Cheat Sheet </entity-cheat-sheet/>`. If you're someone who learns best by doing, you may prefer to jump straight into the tutorial on :doc:`building a community reporting tool with Entities <tutorial-community-reporting>` and to come back here if you have any questions. If you have a question that we haven't answered, you can `post on the forum <https://forum.getodk.org/c/support/6>`_.
1010

1111
**Topics**
1212

docs/entities-reference.rst

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Entities Reference
2+
==================
3+
4+
This is a collection of resources and tutorials for working with Entities in ODK.
5+
6+
.. toctree::
7+
:maxdepth: 2
8+
9+
entity-cheat-sheet
10+
entities-intro
11+
tutorial-community-reporting
12+
central-entities

docs/entity-cheat-sheet.rst

+255
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
.. spelling:word-list::
2+
hh
3+
num
4+
5+
6+
Entity Cheat Sheet
7+
=================================
8+
9+
This page provides a quick reference for how to design forms in :doc:`XLSForm </tutorial-first-form/>` for ODK that use, create, and update :doc:`Entities </entities-intro/>`.
10+
11+
Creating and Updating Entity Data
12+
---------------------------------
13+
14+
When you are designing your form, you can specify how Entities should be created from submissions.
15+
16+
To make a form that creates or updates Entities, there are two places to make changes in your form: the ``survey`` sheet and the ``entities`` sheet.
17+
18+
19+
Survey Sheet
20+
____________
21+
22+
* Add a ``save_to`` column.
23+
24+
* We recommend putting the ``save_to`` column to the right of the ``required`` column (instead of at the end) so it is easier to see.
25+
26+
* For each field you want to save to an Entity, fill out the corresponding Entity *property name* in this column.
27+
28+
* The property name can be different from the field name.
29+
* Not every field needs to be saved to a property. Only fill out the fields you need for your Entity workflow.
30+
31+
32+
.. note::
33+
Entity property names follow the same naming rules as form field names. Additionally, the property names ``name``, ``label``, and anything beginning with ``__`` (two underscores) are not allowed.
34+
35+
.. rubric:: XLSForm
36+
37+
.. csv-table:: Example ``survey`` sheet with ``save_to`` column filled in with property names for certain fields
38+
:header: type, name, label, ..., save_to
39+
40+
geopoint, location, Tree Location, ..., location
41+
text, species, Tree Species, ..., species
42+
text, intake_notes, Intake Notes, ...,
43+
44+
Entities Sheet
45+
______________
46+
47+
The ``entities`` sheet is included in the `XLSForm template <https://docs.google.com/spreadsheets/d/1v9Bumt3R0vCOGEKQI6ExUf2-8T72-XXp_CbKKTACuko>`_, but you can add it yourself if your form does not have one. The required column headers include ``list_name`` and ``label`` but there are several optional column headers depending on your desired functionality.
48+
49+
50+
* Under ``list_name``, write the name of your Entity List.
51+
52+
* Under ``label``, write how to build a label for each Entity.
53+
54+
* We recommend using :doc:`concat </form-logic/>` with fields from your form.
55+
* The label can be blank if the form updates the Entity but does not change the label.
56+
57+
58+
.. rubric:: XLSForm
59+
60+
.. csv-table:: Minimal ``entities`` sheet for creating an Entity
61+
:header: list_name, label
62+
63+
trees, "concat(""Tree: "", ${species})"
64+
65+
* **Optional**
66+
67+
* Column header ``create_if`` with a boolean expression.
68+
* Column header ``update_if`` with a boolean expression.
69+
* Column header ``entity_id`` with the ID of an existing Entity to update.
70+
71+
* Use ``coalesce(${existing_item},uuid())`` if designing a form that both creates and updates Entities.
72+
73+
74+
.. rubric:: XLSForm
75+
76+
.. csv-table:: Example ``entities`` sheet for conditionally creating an Entity
77+
:header: list_name, label, create_if
78+
79+
trees, "concat(""Tree: "", ${species})", ${tree_cm} > 20
80+
81+
82+
.. rubric:: XLSForm
83+
84+
.. csv-table:: Example ``entities`` sheet for conditionally updating an Entity
85+
:header: list_name, label, update_if, entity_id
86+
87+
orders, "Approved: ${existing_order}", ${status} = 'approved', ${existing_order}
88+
89+
90+
.. rubric:: XLSForm
91+
92+
.. csv-table:: Example ``entities`` sheet for creating and updating Entities in the same form
93+
:header: list_name, label, create_if, update_if, entity_id
94+
95+
trees, "concat(""Tree: "", ${species})", ${create_or_update} = 'create', ${create_or_update} = 'update', coalesce(${existing_item},uuid())
96+
97+
98+
.. note::
99+
Current limitation: Only one Entity List can be updated per form and each submission can only create or update a single Entity.
100+
101+
102+
Saving the Entity ID in a Registration Form
103+
___________________________________________
104+
105+
Depending on your workflow, it may be helpful to save the Entity ID (UUID) in the submission data where the Entity is created.
106+
107+
.. rubric:: XLSForm
108+
109+
.. csv-table:: Example of saving the ID of a new Entity in the submission.
110+
:header: type, name, calculation
111+
112+
calculate, new_entity_id, ``/data/meta/entity/@id``
113+
114+
115+
Using Entity Data
116+
-----------------
117+
118+
Entity Lists are used just like CSV attachments. You can use multiple Entity Lists in a single form. There are two main ways to attach an Entity List where **listname** is the name of your Entity List:
119+
120+
#. Use ``select_one_from_file listname.csv`` or ``select_multiple_from_file listname.csv``
121+
122+
* The **.csv** extension after **listname** is necessary.
123+
124+
#. Use ``csv-external`` with ``listname``
125+
126+
.. note::
127+
When you upload your form to Central, it will check the expected attachments and automatically connect an Entity List in place of an attachment when the name matches exactly. You can check what Entity Lists your forms are using by looking at those forms' attachments on Central.
128+
129+
130+
Selecting an Entity
131+
______________________________
132+
133+
When you use ``select_one_from_file listname.csv``, this form field will hold the ID of your selected Entity. This ID is the UUID that Central uses to uniquely track the Entity, e.g. ``4d6a1fe1-6dff-4f72-b122-1413fe9b2dd0``. You might notice UUIDs like this in your submission data.
134+
135+
.. rubric:: XLSForm
136+
137+
.. csv-table:: Example ``survey`` sheet for selecting an Entity with ``select_one_from_file``.
138+
:header: type, name, label
139+
140+
select_one_from_file households.csv, hh_id, Select household
141+
142+
143+
Looking up an Entity from an External CSV
144+
______________________________________
145+
146+
Another way to choose an Entity from a list is by another key. Note that the ``calculate`` to get the ``name`` (also referred to as Entity ID or UUID) is only required if you need to update the Entity.
147+
148+
.. rubric:: XLSForm
149+
150+
.. csv-table:: Example of selecting a household by a barcode ID.
151+
:header: type, name, label, calculation
152+
153+
csv-external, households, ,
154+
barcode, barcode, Scan household barcode,
155+
calculate, hh_id, , instance("households")/root/item[id=${barcode}]/name
156+
157+
158+
Updating a Selected Entity
159+
__________________________
160+
161+
The ID from a ``select_one_from_file`` or the ``name`` property described in the section above is the ID (represented as a UUID) that Central needs when updating the Entity.
162+
163+
.. rubric:: XLSForm
164+
165+
.. csv-table:: Example ``entities`` sheet for updating a selected Entity.
166+
:header: list_name, label, entity_id
167+
168+
household, ,${hh_id}
169+
170+
.. note::
171+
Note that for the example above, leaving ``label`` blank in this update form means it won't be changed when the Entity is updated.
172+
An update form is the only scenario in which ``label`` can be left blank.
173+
This form implicitly updates an Entity because ``entity_id`` is provided and ``create_if`` is not specified.
174+
Refer to the above `Entities Sheet`_ section for more information.
175+
176+
177+
Accessing Entity Data
178+
_____________________
179+
180+
181+
Once an Entity has been selected, you can use that Entity ID to access the properties of that Entity. You can also access the ``__version`` system property of an Entity to know how many updates have been made.
182+
183+
.. rubric:: XLSForm
184+
185+
.. csv-table:: Example of using the ``instance`` function to look up a property of a selected Entity.
186+
:header: type, name, label, calculation
187+
188+
calculate, num_members, ,instance("households")/root/item[name=${hh_id}]/num_members
189+
190+
191+
192+
Pre-filling With Default Values
193+
_______________________________
194+
195+
Note that if you are using ``select_one_from_file`` and want to use the existing value as a default, you will need to use a ``trigger`` to update the value when the Entity is selected.
196+
This follows the pattern of using `dynamic defaults from form data </form-logic/#dynamic-defaults-from-form-data>`_.
197+
198+
.. rubric:: XLSForm
199+
200+
.. csv-table:: Example of using dynamic defaults from form data to pre-fill a field with an Entity property.
201+
:header: type, name, label, save_to, trigger, calculation
202+
203+
integer, num_members, Enter number of household members, num_members, ${hh_id}, instance("households")/root/item[name=${hh_id}]/num_members
204+
205+
206+
207+
Using a Different Key
208+
_____________________
209+
210+
If your Entities have a different important key, you can use the ``parameters`` column to specify a different Entity property as the key. This is useful when you are *not* updating the Entity in the form, and just using the Entity list to manage shared data.
211+
212+
.. rubric:: XLSForm
213+
214+
.. csv-table:: Example of using a different column from your Entity List to serve as the ID or key.
215+
:header: type, name, label, ..., parameters
216+
217+
select_one_from_file states.csv, state, Select state, ..., value=state_id
218+
219+
.. note::
220+
With the example above, you will not be able to use that other key to update the Entity.
221+
This technique works best for read-only data where you are using an Entity List to manage shared data but not updating any Entity data in your form.
222+
223+
Structure of an Entity
224+
----------------------
225+
226+
Entity ID
227+
_________
228+
229+
Every Entity has an ID (a UUID) that is unique across all Entity Lists and projects within Central.
230+
231+
In a form, this Entity ID is accessed through the ``name`` property. This is to fit in with existing CSV attachments and choice lists in which the ``name`` column represents a unique identifier for that row.
232+
233+
In an export and in OData, the Entity ID appears under the ``__id`` column.
234+
235+
236+
Label
237+
_____
238+
239+
Every Entity has a *label* (a non-empty string) that is shown in forms the same way labels for choice lists and CSV attachments are shown.
240+
241+
242+
Properties
243+
__________
244+
245+
Beyond the ID and Label, the properties of your Entity are up to you. Note that ``name`` and the prefix ``__`` cannot be used as property names.
246+
247+
Every value is stored as a string.
248+
249+
We recommend storing the minimal amount of data necessary to drive your workflow.
250+
251+
252+
System Properties
253+
_________________
254+
255+
Every Entity has a ``__version`` number available. Additional system properties such as ``__createdAt``, ``__updatedAt``, ``__createdBy`` are also available on the Entity export and in OData.

docs/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ ODK is open-source software that's made by a welcoming community of people just
5656

5757
form-design-intro
5858
XLSForm Tutorial <tutorial-first-form>
59-
Entities Tutorial <tutorial-community-reporting>
59+
entities-reference
6060
form-reference
6161
form-best-practices
6262

setup.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import sys
2+
if sys.version_info < (3,10):
3+
sys.exit('Sorry, Python < 3.10 is not supported')

0 commit comments

Comments
 (0)