Python 更改django中的表单字段

Python 更改django中的表单字段,python,django,Python,Django,我是Django的初学者。我只是不明白为什么这行不通 我正在尝试使用optgroup及其选项初始化字段 in views.py class SystemDetailView(DetailView): """Detail view for Systems""" form_class = system_form() model = System template_name = 'systems/system_detail.html' def get_context_data(self, **kw

我是Django的初学者。我只是不明白为什么这行不通

我正在尝试使用optgroup及其选项初始化字段

in views.py

class SystemDetailView(DetailView):
"""Detail view for Systems"""

form_class = system_form()
model = System
template_name = 'systems/system_detail.html'

def get_context_data(self, **kwargs):
    context = super(SystemDetailView, self).get_context_data(**kwargs)
    context.update({
        'system_update_form': self.form_class(instance=self.get_object()),
    })

    return context

def system_form(self):
    form= SystemForm
    form.fields['primary_purpose_business_use'].choices= list()
    for optgroup in BusinessSystemType.objects.all():
        form.fields['primary_purpose_business_use'].choices= form.fields['primary_purpose_business_use'].choices.append(
            optgroup.name, list(subtype.name for subtype in BusinessSystemType.objects.filter(parent= optgroup))
        )
    return SystemForm
在forms.py中

class SystemForm(ModelForm):
"""Form to create and modify systems"""

# only show top-level UserTypes for application_user_type field
application_user_type = ModelChoiceField(
       queryset=UserType.objects.filter(parent__isnull=True), required=False,
       help_text="Type of user who interacts with the system")

lines_of_business = ModelMultipleChoiceField(
    widget=forms.widgets.CheckboxSelectMultiple(), queryset=LineOfBusiness.objects.all(),
    required=False, help_text="Identify lines of business this system touches"
)

customer_segments_served = ModelMultipleChoiceField(
    widget=forms.widgets.CheckboxSelectMultiple(), queryset=CustomerSegment.objects.all(),
    required=False, help_text="Customer segments this resource serves"
)

application_user_sub_type = ModelMultipleChoiceField(
    widget=forms.widgets.CheckboxSelectMultiple(), queryset=UserType.objects.filter(parent__name='Internal'),
    required=False, label="Internal user type"
)

primary_purpose_business_use = ModelChoiceField(
       queryset=BusinessSystemType.objects.filter(parent__isnull=True), required=False,
       help_text="primary business purpose")


class Meta:
    model = System
    fields = ['name', 'short_name', 'accountable_team', 'description', 'lines_of_business',
            'business_system_type', 'customer_segments_served',
            'application_user_type', 'other_application_user_type', 'application_user_sub_type',
            'intellectual_property', 'deployment_model',
            'business_criticality', 'service_criticality',
            'vendor', 'other_vendor', 'technology_type', 'other_technology_type',
            'associated_technologies', 'renewal_date',
            'installation_date', 'deprecation_date', 'purchase_price',
            'contract_signed_date', 'contract_end_date', 'parimary_purpose_business_use', 'documentation_url', 'notes', 'tags']
我得到了未解析的引用,其中form\u class=system\u form() 我做错了什么?可能很多。但是你能帮我吗

form_class = system_form()
问题是,系统形式是一种实例方法。这意味着您需要一个SystemDetailView实例,而在创建SystemDetailView时显然没有

似乎您只需要一个函数,该函数返回一个要分配给form_class属性的类。要将方法转换为函数,只需将其移到类主体之外。您也不需要将该
self
作为函数参数


(您的代码显然存在一些缩进问题,但我相信您只是复制并粘贴了错误。)

views.py
中,是否导入了
SystemForm
?是的。但我显然做错了什么。。。。