Package expedient :: Package common :: Package extendable :: Module models :: Class Extendable
[hide private]
[frames] | no frames]

Class Extendable

source code

Extendable object.

An extendable object that enables an instance to be obtained as its class's farthest descendant.

Additionally, fields can be defined that would be added only to a direct subclass. This can be useful to automatically add relationships to subclasses so that relationships exist in the subclasses but not in the parents, allowing the same field name to be used for different types of relationships.

Further, the class that inherits from Extendable may specify which field initialization arguments subclasses may override. Subclasses can override the arguments by adding values to a dict in an inner class.

Adding fields is done by adding an Extend inner class. Three fields can be used in the Extend inner class:

  1. fields: This is a dict that describes what the fields to be added in the subclass are. The format is as follows :

    {field_name : (field_class, args, kwargs, repl_args, repl_kwargs)}

  2. mandatory: iterable of keywords that subclasses must replace.
  3. replacements: dict that specifies the replacements to use in the subclass using the keywords defined in the repl_args and repl_kwargs in a parent's Extend.fields values.
  4. redelegate: iterable of field names from fields in a parent's Extend inner class that specifies which fields should not be added in this class but its subclass.

Using the mandatory field in a child class of Exendable forces grandchildren of to specify replacements for particular keywords.

Grandchildren may further delegate adding fields to their own children i.e. the great grandchildren by specifying the field names in their redelegate field of their inner Extend class.

For example: :

   from django.db import models
   class OtherModel(models.Model): pass

   class OtherModelRel(models.Model):
       parent = models.ForeignKey("TestParent")
       other = models.ForeignKey(OtherModel)
   class YetAnotherModel(models.Model): pass
   class YetAnotherModelRel(models.Model):
       child = models.ForeignKey("TestChild")
       other = models.ForeignKey(YetAnotherModel)
   class TestParent(Extendable):
       class Extend:
           fields = {
               'other': (models.ManyToManyField,
                         {'through': OtherModelRel},
                         {'through': 'other_through'},
           mandatory = [

   class TestChild(TestParent):
       class Extend:
           replacements = {
               'other_model': YetAnotherModel,
               'other_through': YetAnotherModelRel,
Nested Classes [hide private]
Metaclass for all Extendable models.
Instance Methods [hide private]
__init__(self, *args, **kwargs)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
Return this instance as the farthest descendant of its class
source code
is_instance_of(self, klass)
Is the object an instance of the passed class klass?
source code

Inherited from django.db.models.base.Model: __eq__, __hash__, __ne__, __reduce__, __repr__, __str__, clean, clean_fields, date_error_message, delete, full_clean, prepare_database_save, save, save_base, serializable_value, unique_error_message, validate_unique

Inherited from django.db.models.base.Model (private): _collect_sub_objects, _get_FIELD_display, _get_next_or_previous_by_FIELD, _get_next_or_previous_in_order, _get_pk_val, _get_unique_checks, _perform_date_checks, _perform_unique_checks, _set_pk_val

Inherited from object: __delattr__, __format__, __getattribute__, __new__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Class Variables [hide private]
  leaf_name = models.CharField(max_length= 100, blank= True, edi...
  module_name = models.CharField(max_length= 100, blank= True, e...
  _default_manager = <expedient.common.extendable.models.Extenda...
  _meta = <Options for Extendable>

Inherited from django.db.models.base.Model (private): _deferred

Properties [hide private]

Inherited from django.db.models.base.Model: pk

Inherited from object: __class__

Method Details [hide private]

__init__(self, *args, **kwargs)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
(inherited documentation)

Class Variable Details [hide private]


models.CharField(max_length= 100, blank= True, editable= False)


models.CharField(max_length= 100, blank= True, editable= False)


<expedient.common.extendable.models.ExtendableManager object at 0x8afd\