google-code-export / django-mptt

Automatically exported from code.google.com/p/django-mptt
Other
0 stars 0 forks source link

Suggestion to add TreeNodeMultipleChoiceField #70

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I suggest to add this useful field in mptt/forms.py when we have 
ManyToManyField in model:

class TreeNodeMultipleChoiceField(forms.ModelMultipleChoiceField):
    """A ModelMultipleChoiceField for tree nodes."""
    def __init__(self, level_indicator=u'---', *args, **kwargs):
        self.level_indicator = level_indicator
        super(TreeNodeMultipleChoiceField, self).__init__(*args, **kwargs)

    def label_from_instance(self, obj):
        """
        Creates labels which represent the tree level of each node when
        generating option labels.
        """
        return u'%s %s' % (self.level_indicator * getattr(obj,
                                                  obj._meta.level_attr),
                           smart_unicode(obj))

Original issue reported on code.google.com by a.vostrjakov on 19 Apr 2010 at 12:46

GoogleCodeExporter commented 9 years ago

Original comment by craig.ds@gmail.com on 6 Sep 2010 at 9:21

GoogleCodeExporter commented 9 years ago
Added in 
http://github.com/django-mptt/django-mptt/commit/7edad737c9e72a9be4ad04f8f013429
fff7dd343 (some tweaks to the above patch, but usage is the same)

Original comment by craig.ds@gmail.com on 9 Sep 2010 at 9:24