Greetings :)

Let's consider the following two models: -

class State(models.Model):
    short_name = models.CharField(max_length=2, unique=True)
    name = models.CharField(max_length=50, unique=True)
    is_active = models.BooleanField(db_index=True)
    def __unicode__(self):
        return u'%s' % (self.name)

class Company(models.Model):
    name = models.CharField(max_length=50)
    contact_person = models.CharField(max_length=50)
    email = models.EmailField()
    address = models.CharField(max_length=255)
    state = models.ForeignKey(State)
    def __unicode__(self):
        return u'%s' % (self.name)

The following form: -

class AttendToCompanyForm(forms.Form):
    name = forms.CharField(max_length=50, widget=forms.TextInput(attrs=
{'class':'textInput',}))
    contact_person = forms.CharField(max_length=50,
widget=forms.TextInput(attrs={'class':'textInput',}))
    email = forms.EmailField(max_length=75, widget=forms.TextInput
(attrs={'class':'textInput',}))
    address = forms.CharField(max_length=255, widget=forms.Textarea())
    state = forms.ModelChoiceField(State.objects.filter
(is_active=True), widget=forms.Select(attrs={'class':'selectInput',}))

And the view: -

def attend_to_company(request, cid):
    company_to_attend = Company.objects.get(id=cid)
    if request.method == 'POST':
        form = AttendToCompanyForm(request.POST)
        if form.is_valid():
            # some code
            # some code
            # some code
            return HttpResponseRedirect(reverse('redirect-to-some-
page'))
    else:
        data = {'name': agencyrequest_to_attend.name,
                'contact_person':
agencyrequest_to_attend.contact_person,
                'email': agencyrequest_to_attend.email,
                'address': agencyrequest_to_attend.address,
                'state': agencyrequest_to_attend.state.id}
        form = AttendToCompanyForm(data)
    return render_to_response('attend_to_company.html', {'form':
form}, context_instance=RequestContext(request))

When I access the view via HTTP GET, if I use form =
AttendToCompanyForm(data) then the state field (a drop down select) in
the form is populated correctly and the corresponding state for the
company appears as selected in the list, but when I use form =
AttendToCompanyForm(initial=data) then the state field does not
reflect the corresponding state for the company, and instead shows the
first "-----" element of the drop down list.

Is this expected behavior? What is the cause of this?

Thanks and regards,
CM.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to