基于ForeignKey的动态Django表单域

2024-09-30 04:36:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为给定问题组中的每个问题创建一个表单域。我对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公司名称:

^{pr2}$

在表单.py公司名称:

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

我得到“'ResponseForm'对象没有属性''u errors'”。思想?在


Tags: py名称表单modelmodels公司contentlength
1条回答
网友
1楼 · 发布于 2024-09-30 04:36:12

不确定这是否是这里的问题,但您可能应该在重写的\uyu init\uu中调用父窗体初始化方法:

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

    for q in args:
        q.question_content = forms.CharField(max_length = 100)

相关问题 更多 >

    热门问题