2012-07-01 32 views

回答

0

这是一个很好的问题; django似乎很坚持你在任何地方使用相同的评论表单。你可以编写一个单一的表单,根据它实例化的对象显示不同的字段。试着写出一个init方法沿着这条线:

class CustomCommentForm(CommentForm): 
    custom_field = forms.CharField(max_length=100) 

    def __init__(self, *args, **kwargs): 
     super(CustomCommentForm, self).__init__(*args, **kwargs) 

     # check what's in kwargs['initial'], and insert fields if needed like this: 
     if ...: 
      self.fields['optional_field'] = forms.CharField(max_length=100)