python
  • django
  • 2016-06-16 120 views 2 likes 
    2

    我是Django的初學者。我只是失去了爲什麼這不起作用。在Django中改變表單字段

    我試圖初始化OPTGROUP領域及其選項

    在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_class = system_form() 我在做什麼錯了?可能很多。但你能幫我嗎?

    +0

    在'views.py'你導入'SystemForm'? –

    +0

    是的,我做到了。但我很清楚在這裏做錯了什麼.... – PowerLove

    回答

    1
    form_class = system_form() 
    

    的事情是,system_form實例方法。這意味着稱它爲SystemDetailView的實例,而在創建SystemDetailView時顯然沒有。

    看來你只需要一個函數返回一個類來分配給form_class屬性。要將該方法轉換爲函數,只需將其移到類體外。你也不需要那個self作爲函數參數。

    (另外你的代碼顯然有一些壓痕問題,但我相信你只是複製和粘貼錯了。)

    相關問題