Ed.
Ed.

Reputation: 4577

Exclude form field when calling form from view

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.

Is there a way to suppress the "title" field when I call the form? I could create two form classes in my forms.py, but this seems unnecessarily repetitious.

Upvotes: 3

Views: 996

Answers (1)

zsquare
zsquare

Reputation: 10146

Write a constructor for the form class

def __init__ (self, show_title=True):
    super (BaseClass, self).__init__()
    if not show_title:
        del self.fields['title']

Upvotes: 3

Related Questions