Debian Codegraph @

docs/dev/plugins/other/fields.rst

raw
1 Flexible Field Types
2 ====================
3
4 If your plugin uses flexible fields to store numbers or other non-string values,
5 you can specify the types of those fields. A rating plugin, for example, might
6 want to declare that the ``rating`` field should have an integer type:
7
8 .. code-block:: python
9
10 from beets.plugins import BeetsPlugin
11 from beets.dbcore import types
12
13
14 class RatingPlugin(BeetsPlugin):
15 item_types = {"rating": types.INTEGER}
16
17 @property
18 def album_types(self):
19 return {"rating": types.INTEGER}
20
21 A plugin may define two attributes: ``item_types`` and ``album_types``. Each of
22 those attributes is a dictionary mapping a flexible field name to a type
23 instance. You can find the built-in types in the ``beets.dbcore.types`` and
24 ``beets.library`` modules or implement your own type by inheriting from the
25 ``Type`` class.
26
27 Specifying types has several advantages:
28
29 - Code that accesses the field like ``item['my_field']`` gets the right type
30 (instead of just a string).
31 - You can use advanced queries (like :ref:`ranges <numericquery>`) from the
32 command line.
33 - User input for flexible fields may be validated and converted.
34 - Items missing the given field can use an appropriate null value for querying
35 and sorting purposes.