Sung Kang Sung Kang - 2 years ago 117
Python Question

Altering form fields in django

I'm a beginner in Django. I'm just lost why this wouldn't work.

I'm trying to initialize fields with optgroup and its options


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)
'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(, list( for subtype in BusinessSystemType.objects.filter(parent= optgroup))
return SystemForm


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']

I'm getting unresolved reference where form_class = system_form()
What am I doing wrong? Probably a lot. but would you please help me?

Answer Source
form_class = system_form()

The thing is, system_form is an instance method. It means to call it you need an instance of SystemDetailView while you clearly don't have one while creating SystemDetailView.

It seems you just need a function that returns a class to assign to form_class attribute. To turn the method into a function just move it outside the class body. You also won't need that self as a function argument.

(Also your code clearly has some indentation problem, but I believe you just copied and pasted it wrong.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download