Added basic docs for entities and attributes.
[philo.git] / docs / models / entities.rst
1 Entities and Attributes
2 =======================
3
4 One of the core concepts in philo is the relationship between the :class:`Entity` and :class:`Attribute` classes. :class:`Attribute`\ s represent an arbitrary key/value pair by having one :class:`GenericForeignKey` to an :class:`Entity` and another to an :class:`AttributeValue`.
5
6 Functions
7 ---------
8
9 .. function:: register_value_model(model)
10 .. function:: unregister_value_model(model)
11
12    Helper functions to register/unregister a model as a valid content type for a :class:`ForeignKeyValue` or :class:`ManyToManyValue`.
13
14
15 Classes
16 -------
17
18 .. class:: AttributeValue
19
20    This is an abstract base class for models that can be used as values for :class:`Attribute`\ s. See the code for examples of implementations.
21
22    .. attribute:: attribute_set
23
24       :class:`GenericRelation` back to :class:`Attribute`
25
26    .. method:: set_value(value)
27
28       Interpret ``value`` and set the appropriate fields so that the value displayed to the world is equivalent to ``value``.
29
30    .. method:: value_formfields(**kwargs)
31
32       Define any formfields that would be used to construct an instance of this value.
33
34    .. method:: construct_instance(**kwargs)
35
36       Apply cleaned data from the formfields generated by valid_formfields to oneself.
37
38 .. class:: Attribute
39
40    Represents an arbitrary key/value pair attached to a model.
41
42    .. attribute:: entity
43
44       :class:`GenericForeignKey` to anything.
45
46    .. attribute:: value
47
48       :class:`GenericForeignKey` to a subclass of :class:`AttributeValue`.
49
50    .. attribute:: key
51
52       :class:`CharField` containing a key (up to 255 characters) consisting of alphanumeric characters and underscores.
53
54 .. class:: Entity
55
56    A class that simplifies access to related attributes.
57
58    .. attribute:: attribute_set
59
60       :class:`GenericRelation` back to :class:`Attribute`.
61
62    .. attribute:: attributes
63
64       Property that returns a dictionary-like object which can be used to retrieve :class:`Attribute`\ s values directly.
65
66       Example::
67
68          >>> attr = entity.attribute_set.get(key='spam')
69          >>> attr.value.value
70          u'eggs'
71          >>> entity.attributes['spam']
72          u'eggs'