8

I have a form that enters data to db. I have another form with a drop down field that uses the data entered by the first form.

So when I submit data from the first form,the db is updated properly. But when I load the second form the drop down is not updated with the latest data.

Steps followed for debugging

The problem is not with transaction/commit etc. The query to retrieve the data for the drop down in second form is correct.

The problem is not with view cache either(cos we don't have any cache middleware) I also tried the cache decorators like @never_cahce,@cache_control etc

I tried giving a print statement in second form. I believe that the problem is with form cache. Every django form is loaded only once,ie. while loading the first page of the site. Afterwards the form is loaded from this cache.

First page

form

class AddOrganization(forms.Form):   

    orgList = getOrgUnitList()     

    orgUnit = forms.CharField(label=u'Organization Name',
                                max_length=50,
                                error_messages={'required':'Organization name is required field.'})

    parentOrg= forms.ChoiceField(label=u'Parent Organization',
                           choices=[(u'Select',u'Select')]+orgList,
                           error_messages={'required':'Organization name is required field.'})

Second page

form

class AddUser(forms.Form):    

    orgUnitList = getOrgUnitList()        

    email = forms.EmailField(label=u'Email',
                             max_length=50,
                             error_messages={'required':'Email is required field'})  

    orgUnit = forms.ChoiceField(label=u'Organizational Unit',   
                      choices=orgUnitList,                        
                                error_messages={'required':'Organizational unit is required field'})    

Query

def getOrgUnitList():
    orgUnitList = list(OrganizationUnit.objects.values_list('OrgUnitID','OrgUnitName').order_by('OrgUnitName'))
    return orgUnitList

EDIT

Everything is just fine if I use modelforms.Why So?

2
  • Could you show us some code samples. N.B. You will have to reload/fetch the data from the db from your first form which you need display in the dropdown of your second form. Commented Dec 13, 2011 at 7:43
  • We are mere mortals - please provide actual code or else we are just guessing at the problem. Where exactly is this query? Are you positive it's being executed? Even if you are positive, show us! Commented Dec 13, 2011 at 7:49

1 Answer 1

8

The issue is the declaration of orgUnitList as a class property in the form. This means it's called once, when the form is originally defined. So no new elements will be seen, until the server process is restarted.

One way to fix this would be to call the getOrgUnitList function inside the __init__ method of the form:

class AddOrganization(forms.Form):
    ...
    def __init__(self, *args, **kwargs):
        super(AddOrganizationForm, self).__init__(*args, **kwargs)
        self.fields['orgUnit'].choices = getOrgUnitList()

Alternatively, you should look into using ModelChoiceField for orgUnit, as it deals with this sort of thing automatically.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.