The issue is that CLIENT_CHOICES is only evaluated once, when the
server is started an all the code is loaded into memory. Try wrapping
it all in a function, thusly:

def CLIENT_CHOICES()
    choices = [
        ('', 'Select Client'),
    ]
    clients = CustomerMaster.objects.all().order_by('customer_name')
    for client in clients:
        choices.extend([(client.id, client.customer_name)])
    return choices

class CreateForm(forms.Form):
        client = forms.ChoiceField(choices=CLIENT_CHOICES())

Now, every time the model is loaded, CLIENT_CHOICES() will be called
and you'll get fresh values.

HTH.
--Derek

On Jan 20, 10:47 am, Dheeraj Irukulla <dheer...@gmail.com> wrote:
> Hi Malcolm,
>
> I'm building my forms in a file called forms.py.  This is how I populate the
> choice list:
>
> CLIENT_CHOICES = [
>         ('', 'Select Client'),
> ]
>
> clients = CustomerMaster.objects.all().order_by('customer_name')
> for client in clients:
>         CLIENT_CHOICES.extend([(client.id, client.customer_name)])
>
> This is how I build my form:
>
> class CreateForm(forms.Form):
>         client = forms.ChoiceField(choices=CLIENT_CHOICES)
>
> It sounds like I am doing what you mentioned below.  How should I be doing
> this?
>
> Thanks!
> Raj
>
> On Fri, Jan 16, 2009 at 7:42 PM, Malcolm Tredinnick <
>
> malc...@pointy-stick.com> wrote:
>
> > On Fri, 2009-01-16 at 16:26 -0800, raji wrote:
> > > Hey django-users,
>
> > > I've got a form in my django app that contains a select box that is
> > > dynamically generated from a db object.  In this case, it's a list of
> > > clients.
>
> > > When a user adds to the client list (via another django app), the
> > > 'client' select box isn't updated with the new value(s) the next time
> > > it is loaded.  This sounds like a caching issue.
>
> > Or one of about half a dozen other things.
>
> > How are you constructing the form? In particular, *where* are you
> > constructing the form. For example, a common mistake is to populate a
> > choices list in a module-level form class via a database query, so the
> > query is only run once, at import time.
>
> > Please post a short fragment of the code showing how/where you are
> > creating the form and how you are attempting to use it.
>
> > Regards,
> > Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to