Entities and Attributes

One of the core concepts in Philo is the relationship between the Entity and Attribute classes. Attributes represent an arbitrary key/value pair by having one GenericForeignKey to an Entity and another to an AttributeValue.

Attributes

class philo.models.base.Attribute(*args, **kwargs)

Attributes exist primarily to let arbitrary data be attached to arbitrary model instances without altering the database schema and without guaranteeing that the data will be available on every instance of that model.

Generally, Attributes will not be accessed as models; instead, they will be accessed through the Entity.attributes property, which allows direct dictionary getting and setting of the value of an Attribute with its key.

entity

GenericForeignKey to anything (generally an instance of an Entity subclass).

value

GenericForeignKey to an instance of a subclass of AttributeValue as determined by the attribute_value_limiter.

key

CharField containing a key (up to 255 characters) consisting of alphanumeric characters and underscores.

set_value(value, value_class=<class 'philo.models.base.JSONValue'>)

Given a value and a value class, sets up self.value appropriately.

class philo.models.base.AttributeValue(*args, **kwargs)

This is an abstract base class for models that can be used as values for Attributes.

AttributeValue subclasses are expected to supply access to a clean version of their value through an attribute called “value”.

set_value(value)

Given a value, sets the appropriate fields so that it can be correctly stored in the database.

value_formfields(**kwargs)

Returns any formfields that would be used to construct an instance of this value.

Returns:A dictionary mapping field names to formfields.
construct_instance(**kwargs)

Applies cleaned data from the formfields generated by valid_formfields to oneself.

philo.models.base.attribute_value_limiter

An instance of ContentTypeSubclassLimiter which is used to track the content types which are considered valid value models for an Attribute.

class philo.models.base.JSONValue(*args, **kwargs)

Bases: philo.models.base.AttributeValue

Stores a python object as a json string.

class philo.models.base.ForeignKeyValue(*args, **kwargs)

Bases: philo.models.base.AttributeValue

Stores a generic relationship to an instance of any value content type (as defined by the value_content_type_limiter).

class philo.models.base.ManyToManyValue(*args, **kwargs)

Bases: philo.models.base.AttributeValue

Stores a generic relationship to many instances of any value content type (as defined by the value_content_type_limiter).

philo.models.base.value_content_type_limiter

An instance of ContentTypeRegistryLimiter which is used to track the content types which can be related to by ForeignKeyValues and ManyToManyValues.

philo.models.base.register_value_model(model)

Registers a model as a valid content type for a ForeignKeyValue or ManyToManyValue through the value_content_type_limiter.

philo.models.base.unregister_value_model(model)

Registers a model as a valid content type for a ForeignKeyValue or ManyToManyValue through the value_content_type_limiter.

Entities

class philo.models.base.Entity(*args, **kwargs)

An abstract class that simplifies access to related attributes. Most models provided by Philo subclass Entity.

class philo.models.base.TreeEntityManager
get_with_path(path, root=None, absolute_result=True, pathsep='/', field='pk')

If absolute_result is True, returns the object at path (starting at root) or raises an ObjectDoesNotExist exception. Otherwise, returns a tuple containing the deepest object found along path (or root if no deeper object is found) and the remainder of the path after that object as a string (or None if there is no remaining path).

Note

If you are looking for something with an exact path, it is faster to use absolute_result=True, unless the path depth is over ~40, in which case the high cost of the absolute query may make a binary search (i.e. non-absolute) faster.

Note

SQLite allows max of 64 tables in one join. That means the binary search will only work on paths with a max depth of 127 and the absolute fetch will only work to a max depth of (surprise!) 63. Larger depths could be handled, but since the common use case will not have a tree structure that deep, they are not.

Parameters:
  • path – The path of the object
  • root – The object which will be considered the root of the search
  • absolute_result – Whether to return an absolute result or do a binary search
  • pathsep – The path separator used in path
  • field – The field on the model which should be queried for path segment matching.
Returns:

An instance if absolute_result is True or an (instance, remaining_path) tuple otherwise.

Raises django.core.exceptions.ObjectDoesNotExist:
 

if no object can be found matching the input parameters.

class philo.models.base.TreeEntity(*args, **kwargs)

Bases: philo.models.base.Entity, mptt.models.MPTTModel

An abstract subclass of Entity which represents a tree relationship.

objects

An instance of TreeEntityManager.

get_path(root=None, pathsep='/', field='pk', memoize=True)
Parameters:
  • root – Only return the path since this object.
  • pathsep – The path separator to use when constructing an instance’s path
  • field – The field to pull path information from for each ancestor.
  • memoize – Whether to use memoized results. Since, in most cases, the ancestors of a TreeEntity will not change over the course of an instance’s lifetime, this defaults to True.
Returns:

A string representation of an object’s path.

get_path(root=None, pathsep='/', field='pk', memoize=True)
Parameters:
  • root – Only return the path since this object.
  • pathsep – The path separator to use when constructing an instance’s path
  • field – The field to pull path information from for each ancestor.
  • memoize – Whether to use memoized results. Since, in most cases, the ancestors of a TreeEntity will not change over the course of an instance’s lifetime, this defaults to True.
Returns:

A string representation of an object’s path.

path
Parameters:
  • root – Only return the path since this object.
  • pathsep – The path separator to use when constructing an instance’s path
  • field – The field to pull path information from for each ancestor.
  • memoize – Whether to use memoized results. Since, in most cases, the ancestors of a TreeEntity will not change over the course of an instance’s lifetime, this defaults to True.
Returns:

A string representation of an object’s path.

Project Versions

Table Of Contents

Previous topic

Philo’s models

Next topic

Nodes and Views: Building Website structure

This Page