2016-06-16 11 views
2

私はDjangoの初心者です。なぜこれがうまくいかないのか分かりません。djangoのフォームフィールドを変更する

私はどこform_class未解決の参照を取得していますOPTGROUPとviews.py

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

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 

でそのオプション

でフィールドを初期化しようとしている

= system_form() 私は何が間違っていますか?おそらくたくさん。しかし、私を助けてくれますか?

+0

views.py' 'であなたは' SystemForm'をインポートしたのですか? –

+0

はい、私はしました。しかし、私は明らかにここで何か間違っている.... – PowerLove

答えて

1
form_class = system_form() 

ものであり、system_formインスタンスメソッドあります。それはSystemDetailViewのインスタンスが必要であると呼んでいますが、明らかに作成していない間にSystemDetailViewという名前を付けることを意味します。

form_class属性に割り当てるクラスを返す関数が必要なようです。メソッドを関数に変換するには、クラス本体の外に移動します。また、関数の引数としてselfは必要ありません。

(また、あなたのコードは明らかにいくつかのインデントの問題を抱えているが、私はあなたはそれが間違ってコピー&ペーストと信じています。)

関連する問題