Thanks again.  Just following up with my model to show how I'm
implementing based on the advice above.

Note I call my categories model Topics instead but you get the
picture.

class Topic(models.Model):
    '''
    Topics form sections and categories of posts to enable topical
based
    conversations.

    '''
    text = models.CharField(max_length=75)
    parent = models.ForeignKey('self', null=True, blank=True) # Enable
topic structures.
    description = models.TextField(null=True, blank=True)
    slug = models.CharField(max_length=75)
    path = models.CharField(max_length=255)

    # Custom manager for returning published posts.
    objects = TopicManager()

    def get_path(self):
        '''
        Constructs the path value for this topic based on hierarchy.

        '''
        ontology = []
        target = self.parent
        while(target is not None):
           ontology.append(target.slug)
           target = target.parent
        ontology.append(self.slug)
        return '/'.join(ontology)


    def save(self, force_insert=False, force_update=False):
        '''
        Custom save method to handle slugs and such.
        '''
        # Set pub_date if none exist and publish is true.
        if not self.slug:
            qs = Topic.objects.filter(parent=self.parent)
            unique_slugify(self, self.text, queryset=qs) # Unique for
each parent.

        # Raise validation error if trying to create slug duplicate
under parent.
        if
Topic.objects.exclude(pk=self.pk).filter(parent=self.parent,
slug=self.slug):
            raise ValidationError("Slugs cannot be duplicated under
the same parent topic.")

        self.path = self.get_path() # Rebuild the path attribute
whenever saved.

        super(Topic, self).save(force_insert, force_update) # Actual
Save method.

    def __unicode__(self):
        '''Returns the name of the Topic as a it's chained
relationship.'''
        ontology = []
        target = self.parent
        while(target is not None):
           ontology.append(target.text)
           target = target.parent
        ontology.append(self.text)
        return ' - '.join(ontology)

    class Meta:
        ordering = ['path']
        unique_together = (('slug', 'parent'))


On Apr 7, 8:34 pm, Tim Shaffer <t...@tim-shaffer.com> wrote:
> Oh, check out the Category class from django-simplecms. It implements
> the first method.
>
> Specifically, check out the save() method that builds the path based
> on all the parent categories if it doesn't exist.
>
> http://code.google.com/p/django-simplecms/source/browse/trunk/simplec...

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to