forms.py 446 Bytes
Newer Older
cynddl's avatar
cynddl committed
1
2
3
4
5
6
7
8
9
10
11
from django import forms


class TagMultipleChoiceField(forms.ModelMultipleChoiceField):
    def prepare_value(self, value):
        if hasattr(value, 'tag_id'):
            return value.tag_id
        elif hasattr(value, '__iter__') and not isinstance(value, str) and not hasattr(value, '_meta'):
            return [self.prepare_value(v) for v in value]
        else:
            return super(TagMultipleChoiceField, self).prepare_value(value)