Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 488
Ed. Avatar asked Jan 29 '26 10:01

Ed.


1 Answers

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']
like image 84
zsquare Avatar answered Feb 03 '26 09:02

zsquare