Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango initial value of choicefield
    primarykey
    data
    text
    <p>I'm having a strange problem where I can't seem to set the initial value of one of the fields in my forms in django.</p> <p>My model field is: </p> <pre><code>section = models.CharField(max_length=255, choices=(('Application', 'Application'),('Properly Made', 'Properly Made'), ('Changes Application', 'Changes Application'), ('Changes Approval', 'Changes Approval'), ('Changes Withdrawal', 'Changes Withdrawal'), ('Changes Extension', 'Changes Extension'))) </code></pre> <p>My form code is:</p> <pre><code>class FeeChargeForm(forms.ModelForm): class Meta: model = FeeCharge # exclude = [] # uncomment this line and specify any field to exclude it from the form def __init__(self, *args, **kwargs): super(FeeChargeForm, self).__init__(*args, **kwargs) self.fields['received_date'] = forms.DateField(('%d/%m/%Y',), widget=forms.DateTimeInput(format='%d/%m/%Y', attrs={'class': 'date'})) self.fields['comments'].widget.attrs['class']='html' self.fields['infrastructure_comments'].widget.attrs['class']='html' </code></pre> <p>My view code is:</p> <pre><code>form = FeeChargeForm(request.POST or None) form.fields['section'].initial = section </code></pre> <p>Where section is a url var passed to the function. I've tried:</p> <pre><code>form.fields['section'].initial = [(section,section)] </code></pre> <p>With no luck either :( </p> <p>Any ideas what I'm doing wrong or is there a better way to set the default value (before a form submit) of this choice field from a url var?</p> <p>Thanks in advance!</p> <p><strong>Update:</strong> It seems to be something to do with the URL variable.. If I use:</p> <pre><code>form.fields['section'].initial = "Changes Approval" </code></pre> <p>It works np.. If I HttpResponse(section) it's outputs correctly tho.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload