Filtering needs

Sphinx-needs supports the filtering of need and need_parts by using easy to use options or powerful filter string.

Available options are specific to the used directive, whereas the filter string is supported by all directives and roles, which provide filter capabilities.

Filter options

The following filter options are supported by directives:

Related to the used directive and its representation, the filter options create a list of needs, which match the filters for status, tags, types and filter.

For :status:, :tags: and :types: values are separated by “;”. :filter: gets evaluated.

The logic, if a need belongs to the final result list, is as followed:

status = (open OR in_progress) AND tags = (user OR login) AND types = (req OR spec) AND eval(filter) is True


Use the status option to filter needs by their status.

You can easily filter for multiple statuses by separating them by “;”. Example: open; in progress; reopen.


tags allows to filter needs by one or multiple tags.

To search for multiple tags, simply separate them by using “;”.


For :types: the type itself or the human-readable type-title can be used as filter value.

Show example
.. needtable::
   :types: test
ID Title Status Outgoing
T_C3893 → Test for XY implemented OWN_ID_123; IMPL_01
copy_3 → test of specification and requirement copy_2; copy_1
T_5CCAA → Test 1


Sorts the result list. Allowed values are id and status

Show example
.. needtable::
   :sort_by: id
   :status: open
ID Title Status Outgoing
ID123 → User needs to login open
R_2A9D0 → Requirement A open
R_F4722 → My first requirement open
S_01A67 → Specification B open
copy_1 → copy-example open
copy_2 → copy-example implementation open copy_1
df_1 → my test requirement open
my_car_1 → Car must be awesome open
xyz_123 → My requirement with custom options open
→ my_car_1.1 → A topspeed of 300 km/h open
→ my_car_1.awesome_3 →  a turbo button open
→ my_car_1.2 →  An acceleration of 200 m/s or much much more open


The filter option allows the definition of a complex query string, which gets evaluated via eval() in Python. Please see Filter string for more details.

Filter string

The usage of a filter string is supported/required by:

The filter string must be a valid Python expression:

:need_count:`type=='spec' and status.upper()!='OPEN'`

A filter string gets evaluated on needs and need_parts! A need_part inherits all options from its parent need, if the need_part has no own content for this option. E.g. the need_part title is kept, but the status attribute is taken from its parent need.


Following attributes are kept inside a need_part: id, title, links_back

This allows to perform searches for need_parts, where search options are based on parent attributes.

The following filter will find all need_parts, which are part of a need, which has a tag called important.

:need_count:`is_part and 'important' in tags`

Inside a filter string the following variables/functions can be used:

  • tags as Python list (compare like "B" in tags)
  • type as Python string (compare like "story" == type)
  • status as Python string (compare like "opened" != status)
  • sections as Python list with the hierarchy of sections with lowest-level section first. (compare like "Section Header" in sections)
  • id as Python string (compare like "MY_ID_" in id)
  • title as Python string (compare like len(title.split(" ")) > 5)
  • links as Python list (compare like "ID_123" not in links)
  • links_back as Python list (compare like "ID_123" not in links_back)
  • content as Python string (compare like len(content) == 0)
  • is_need as Python boolean. (compare like is_need)
  • is_part as Python boolean. (compare like is_part)
  • parts as Python list with need_part / np of the current need. (compare like len(parts)>0)
  • search, as Python function for performing searches with a regular expression

Additional variables for need_part / np:

  • id_parent as Python string, which contains the id of the parent need. (compare like id_parent == "ABC_01")
  • id_complete as Python string. Contains the concatenated ids of parent need and need_part. (compare like id_complete != 'ABC_01.03')


If extra options were specified using needs_extra_options then those will be available for use in filter expressions as well.

If your expression is valid and it’s True, the related need is added to the filter result list. If it is invalid or returns False, the related need is not taken into account for the current filter.

Show example
.. req:: Requirement A
   :tags: A; filter_example
   :status: open

.. req:: Requirement B
   :tags: B; filter_example
   :status: closed

.. spec:: Specification A
   :tags: A; filter_example
   :status: closed

.. spec:: Specification B
   :tags: B; filter_example
   :status: open

.. test:: Test 1
   :tags: filter_example

.. needtable::
   :filter: "filter_example" in tags and ("B" in tags or ("spec" == type and "closed" == status)) or "test" == type

This will have the following result:

S_01A67: Specification B
T_5CCAA: Test 1
R_22EB2: Requirement B
S_D70B0: Specification A