0
我按照https://docs.djangoproject.com/en/dev/ref/contrib/comments/custom/指南在我的当前django应用的新闻条目上设置了评论表单。现在,我需要为网站的另一部分中的另一种对象使用不同字段的评论表单。django.contrib.comments和多个评论表格
考虑到我已经覆盖了联系表单,这应该如何实现?
我按照https://docs.djangoproject.com/en/dev/ref/contrib/comments/custom/指南在我的当前django应用的新闻条目上设置了评论表单。现在,我需要为网站的另一部分中的另一种对象使用不同字段的评论表单。django.contrib.comments和多个评论表格
考虑到我已经覆盖了联系表单,这应该如何实现?
这是一个很好的问题; 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)