Version 1 (modified by boxed@…, 8 years ago) (diff)


First 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:

Index: django/core/meta/
--- django/core/meta/	(revision 2013)
+++ django/core/meta/	(working copy)
@@ -1026,6 +1026,11 @@
             ','.join(placeholders)), db_values)
         if opts.has_auto_field and not pk_set:
             setattr(self,, db.get_last_insert_id(cursor, opts.db_table,
+    # Run any pre-commit hooks.
+    if hasattr(self, '_pre_commit'):
+        self._pre_commit(opts, cursor)
     # Run any post-save hooks.
     if hasattr(self, '_post_save'):
@@ -1696,6 +1701,7 @@ = curry(manipulator_save, opts, klass, add, change)
     man.get_related_objects = curry(manipulator_get_related_objects, opts, klass, add, change)
     man.flatten_data = curry(manipulator_flatten_data, opts, klass, add, change)
+    man.Klass = klass
     for field_name_list in opts.unique_together:
         setattr(man, 'isUnique%s' % '_'.join(field_name_list), curry(manipulator_validator_unique_together, field_name_list, opts))
     for f in opts.fields:

Create helper methods for iterating through the parents (like in A "category" data model but I made it more general to avoid code duplication):

def foo_recurse_list(obj, parent_field, value_field):
    parent_list = []
        if getattr(obj, 'parent_'+parent_field+'_id'):
            p = getattr(obj, 'get_parent_'+parent_field)()
            if value_field:
                f = getattr(p, value_field)
            more = recurse_list(p, parent_field, value_field)
        return parent_list

def recurse_list(obj, parent_field, value_field):
    parent_list = foo_recurse_list(obj, parent_field, value_field)
    return parent_list

In 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:

    def _pre_commit(self, opts, cursor):
        # TODO: import above and remove this import line when django gets the no-magic patches
        from where_ever_you_put_the_code_above import recurse_list
        parent_list = recurse_list(self, 'your_model_name_here', 'id')
        if in parent_list:
            raise 'Cannot be a child of itself'

        self.cache_hierarchy = '/'.join([str(i) for i in parent_list])
        cursor.execute("UPDATE %s SET cache_hierarchy = %%s WHERE id=%%s" % (db.quote_name(opts.db_table)), [self.cache_hierarchy,])