Source

flat_forum / flat_forum / forms.py

from django import forms
import re
from flat_forum import models
from taggit.models import TaggedItem

class PostForm(forms.ModelForm):

    def clean(self):
        '''Auto-tag'''
        data = self.cleaned_data
        tags = set([ tag.lower() for tag in data.get('tags', []) ])
        tags.update([
            tag
            for tag in TaggedItem.tags_for(models.Post)
            if re.search(r'\b%s\b' % tag.name, data['content'], re.I|re.M)
        ])
        data['tags'] = list(tags)
        return data

    class Meta:
        model = models.Post
        exclude = ['reply_to', 'posted_by', 'created',]

class TagForm(forms.Form):
    tags = forms.ModelMultipleChoiceField(queryset=models.Post.tags.most_common(),
        widget=forms.CheckboxSelectMultiple,
        required=False
    )