class ContactForm(forms.Form):
    to = forms.EmailField()
    message = forms.CharField(widget=forms.Textarea())
    
    def __init__(self, user, *args, **kwargs):
        super(ContactForm, self).__init__(*args, **kwargs)
        
        # user isn't logged in, so ask him for an email
        from_field = forms.EmailField()
        
        if not user.is_anonymous():
            from_field.widget = forms.HiddenInput
            from_field.initial = user.email
        
        # insert the field at the start of the fields
        new_fields = self.fields.items()
        new_fields.insert(0, ('from', from_field))
        self.fields = SortedDictFromList(new_fields)