Changes between Initial Version and Version 1 of CookBookTreeStructure


Ignore:
Timestamp:
Apr 2, 2006, 1:27:42 PM (18 years ago)
Author:
boxed@…
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CookBookTreeStructure

    v1 v1  
     1First of all there needs to be support for a pre-commit handler if you're using the 0.9x versions of django. The patch for this is:
     2{{{
     3Index: django/core/meta/__init__.py
     4===================================================================
     5--- django/core/meta/__init__.py        (revision 2013)
     6+++ django/core/meta/__init__.py        (working copy)
     7@@ -1026,6 +1026,11 @@
     8             ','.join(placeholders)), db_values)
     9         if opts.has_auto_field and not pk_set:
     10             setattr(self, opts.pk.attname, db.get_last_insert_id(cursor, opts.db_table, opts.pk.column))
     11+
     12+    # Run any pre-commit hooks.
     13+    if hasattr(self, '_pre_commit'):
     14+        self._pre_commit(opts, cursor)
     15+   
     16     db.db.commit()
     17     # Run any post-save hooks.
     18     if hasattr(self, '_post_save'):
     19@@ -1696,6 +1701,7 @@
     20     man.save = curry(manipulator_save, opts, klass, add, change)
     21     man.get_related_objects = curry(manipulator_get_related_objects, opts, klass, add, change)
     22     man.flatten_data = curry(manipulator_flatten_data, opts, klass, add, change)
     23+    man.Klass = klass
     24     for field_name_list in opts.unique_together:
     25         setattr(man, 'isUnique%s' % '_'.join(field_name_list), curry(manipulator_validator_unique_together, field_name_list, opts))
     26     for f in opts.fields:
     27}}}
     28
     29Create helper methods for iterating through the parents (like in [wiki:CookBookCategoryDataModel A "category" data model] but I made it more general to avoid code duplication):
     30{{{
     31def foo_recurse_list(obj, parent_field, value_field):
     32    parent_list = []
     33    try:
     34        if getattr(obj, 'parent_'+parent_field+'_id'):
     35            p = getattr(obj, 'get_parent_'+parent_field)()
     36            if value_field:
     37                f = getattr(p, value_field)
     38                parent_list.append(f)
     39            else:
     40                parent_list.append(p)
     41            more = recurse_list(p, parent_field, value_field)
     42            parent_list.extend(more)
     43    finally:
     44        return parent_list
     45
     46def recurse_list(obj, parent_field, value_field):
     47    parent_list = foo_recurse_list(obj, parent_field, value_field)
     48    parent_list.reverse()
     49    return parent_list
     50}}}
     51
     52In your model you will have a foreign key pointing to the parent element of each element and a character field that will be used to store the structure (called "cache_hierarchy" in my code). Then add a _pre_commit handler like this:
     53{{{
     54    def _pre_commit(self, opts, cursor):
     55        # TODO: import above and remove this import line when django gets the no-magic patches
     56        from where_ever_you_put_the_code_above import recurse_list
     57        parent_list = recurse_list(self, 'your_model_name_here', 'id')
     58        if self.id in parent_list:
     59            raise 'Cannot be a child of itself'
     60       
     61        parent_list.append(self.id)
     62
     63        self.cache_hierarchy = '/'.join([str(i) for i in parent_list])
     64       
     65        cursor.execute("UPDATE %s SET cache_hierarchy = %%s WHERE id=%%s" % (db.quote_name(opts.db_table)), [self.cache_hierarchy, self.id])
     66}}}
Back to Top