0

我正在尝试为给定问题组中的每个问题创建一个表单字段。我对 python/django 很陌生,所以这可能是一个不好的方法,欢迎提出高级建议。这是我所拥有的:

模型.py:

class QuestionGroup(models.Model):
    name = models.CharField(max_length = 50)
    creator = models.ForeignKey(User)
    modified = models.DateTimeField()

class Question(models.Model):
    question_content = models.CharField(max_length = 100)
    question_group = models.ForeignKey(QuestionGroup)

视图.py:

def give_feedback(request, group_id):
    question_list = Question.objects.filter(question_group = group_id)
    form = ResponseForm(question_list)

表格.py:

class ResponseForm(forms.Form):
    def __init__(self, *args, **kwargs):
        for q in args:
            q.question_content = forms.CharField(max_length = 100)

我得到“'ResponseForm' 对象没有属性'_errors'”。想法?

4

1 回答 1

2

不确定是否是这里的问题,但您可能应该在覆盖的 __init__ 中调用父表单初始化方法:

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

    for q in args:
        q.question_content = forms.CharField(max_length = 100)
于 2013-09-08T03:00:15.170 回答