King
King

Reputation: 451

Django link form to fields to model

Im abit confused on how to link form fields to the model field.

I wanted to create a radio button that yes stands for "defect" and no for "No Defect.

So in the form i created the following:

CHOICES = [('Defect', 'yes'),
               ('No Defect', 'no')]
self.fields['Audit_outcome'].widget = forms.ChoiceField(choices=CHOICES)

But when i do that i get the following error:

'ChoiceField' object has no attribute 'attrs'

I then wanted to call that field in the HTML page so the inputs selected there get updated... but im abit stuck

Forms.py:

class createUserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']


class auditForm(ModelForm):
    CHOICES = [('Defect', 'yes'),
               ('No Defect', 'no')]
    Audit_outcome = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect)
    
    class Meta:
        model = datas
        fields = '__all__'
        
    def __init__(self, *args, **kwargs):
        super(auditForm, self).__init__(*args, **kwargs)
        # Makes the field un-editable
        

        self.fields['qs_login'].disabled = True
        self.fields['associate_resolve_date'].disabled = True
        self.fields['seller_id'].disabled = True
        self.fields['Task_ID'].disabled = True
        self.fields['associate_queue'].disabled = True
        self.fields['associate'].disabled = True
        self.fields['metric_wk_no'].disabled = True
        self.fields['associate_action'].disabled = True
        

        #IPV COMMENT BOX
        self.fields['QA_Comments_on_Associate_Action'].widget = forms.Textarea(
            attrs={'class': 'form-control', 'style': 'height: 100px'}) #Sets the size of the widget
        #SIV COMMENT BOX
        self.fields['QA_Comments_on_investigator_Action'].widget = forms.Textarea(
            attrs={'class': 'form-control', 'style': 'height: 100px'}) #Sets the size of the widget

        CHOICES = (('Defect', 'yes'),
               ('No Defect', 'no'))
        self.fields['Audit_outcome'].widget = forms.ChoiceField(choices=CHOICES)

        for field in self.fields:
            self.fields[field].required = False
            if field != "qs_login" and field != "Status" and field != "associate_resolve_date" and field != "QA_Comments_on_Associate_Action":
                self.fields[field].widget.attrs['class'] = "form-control"




        x = datas.objects.all()
        #y = datas.objects.get(task_id=pk)


        self.fields['country'].disabled = True 
        self.fields['associate_resolve_date'].disabled = True
        
        self.fields['QA_Comments_on_Associate_Action'].widget = forms.Textarea(
            attrs={'class': 'form-control', 'style': 'height: 100px'}) #Sets the size of the widget
        for field in self.fields:
            self.fields[field].required = False
            if field != "qs_login" and field != "Status" and field != "associate_resolve_date" and field != "QA_Comments_on_Associate_Action":
                self.fields[field].widget.attrs['class'] = "form-control"

The HTML RADIO BUTTON SECTION:

<p>
                    Defect?
                    <input type="radio" name="radAnswer" id="defect-yes"/>
                    <label for="defect-yes">Yes</label>
                    <input type="radio" name="radAnswer" id="defect-no"/>
                    <label for="defect-no">No</label>
                </p>
                <p>
                    Action Correctly Captured?
                    <input type="radio" name="radAnswer2" id="defect-yes"/>
                    <label for="defect-yes">Yes</label>
                    <input type="radio" name="radAnswer2" id="defect-no"/>
                    <label for="defect-no">No</label>
                </p>
                <p>

Upvotes: 1

Views: 529

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477794

A ChoiceField is not a widget, this is a form field. The default widget for a ChoiceField is the Select widget [Django-doc].

A form field specifies what will be retrieved/rendered for the form, and the widget determines how (with what HTML logic) the client can write input.

You thus can specify this widget with:

CHOICES = [('Defect', 'yes'), ('No Defect', 'no')]
self.fields['Audit_outcome'].widget = forms.Select(choices=CHOICES)

Upvotes: 2

Related Questions