0

我有以下表格:

class EmailPreferences(forms.ModelForm):
    """ Base form used for fields that are always required """
    def __init__(self, *args, **kw):
        super(EmailPreferences, self).__init__(*args, **kw)

    class Meta:
        model = teacher_email_preferences
        exclude = ['id', 'c_user']

    def save(self, force_insert=False, force_update=False, commit=True):
        obj = super(EmailPreferences, self).save(commit=commit)
        return obj

如您所见,型号名称是固定的,即teacher_email_preferences. 但我的网站有两种类型的用户,一种TeacherStudent. 我不想创建单独的表单。所以我想student_email_preferences在启动此表单时将模型更改为。做的时候如何在这里传递模型form = EmailPreferences()

4

1 回答 1

1

你不能。但是您可以改用闭包。

def emailform(emailmodel):
    class EmailPreferences(forms.ModelForm):
         ...

        class Meta:
            model = emailmodel

         ...

    return EmailPreferences

 ...

form = emailform(teacher_email_preferences)()
于 2012-05-28T09:39:41.387 回答