Maybe you could just use subclassing instead of doing stuff at run-time:

class UploadForm(forms.Form):
    file = ...
    # custom upload and validation code here

class ThumbnailUploadForm(UploadForm):
    pass

class UploadFileForm(UploadForm):
    title = ...

As for your current approach, it looks correct. I've done the same
several times and it works as expected. Make sure you are really
really passing show_title correctly in your thumbnail case.

Regards
Knut

On Mon, Nov 8, 2010 at 9:29 PM, Ed <edmund.rog...@gmail.com> wrote:
> I have an image upload form that takes a title and a file for its
> field. I have two uses for it. Most of the time I call it, I need both
> a title and the image itself. But when I call it simply to grab a
> thumbnail, I don't need the title. In fact, the form data is saved to
> a different model that doesn't even have title as a field.
>
> I wanted to suppress the "title" field when I call the form. I could
> have created two form classes in my forms.py, but this seemed
> unnecessarily repetitious.
>
> I included the following in my image form class:
>
> def __init__ (self, show_title=True):
>    super (BaseClass, self).__init__()
>    if not show_title:
>        del self.fields['title']
>
> This works great except for one thing. Now I'm getting validation
> errors: "This field is required" whether I suppress the title field or
> not. Any time I try to submit the form, it tells me I need to enter
> data.  Any advice on how to do this correctly?
>
> class UploadFileForm(forms.Form):
>    def __init__ (self, show_title=True):
>        super (BaseClass, self).__init__()
>        if not show_title:
>            del self.fields['title']
>
>    title = forms.CharField(max_length=50)
>    file = forms.ImageField(label='Select photo to upload')
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to