forms.py 598 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)
12 13 14 15 16 17

    def clean(self, value):
        value = self.to_python(value)
        self.validate(value)
        self.run_validators(value)
        return value