|
List Info
Thread: cache problem of new forms: make ChoicesField's choices list on fly
|
|
| cache problem of new forms: make
ChoicesField's choices list on fly |
  United States |
2007-04-23 08:44:54 |
Hi there~
I have a ChoiceField() of [newforms] in CancelForm,
it's choices list is depending on database table sign2.
my source code is below.
--
from django import newforms as forms
class CancelForm(forms.Form):
U_hash = {}
for U in Sign2.objects.all():
U_hash[U.unit.id] = U.unit.name
Units = [('', '')]
for (key, value) in U_hash.items():
unit = (key, value)
Units.append(unit)
name = forms.CharField(label="name",
max_length=10)
no = forms.CharField(label="personalid",
max_length=10)
unit = forms.ChoiceField(label="unitname",
choices=Units)
--
The problem is:
When start apache server, the sign2 table only have two
unit's
records. after some minutes, may be the unit's records
grows, but the
CancelForm only have two choices. I must restart apache
server to
renew the CancelForm.
thanks for your patience.
--~--~---------~--~----~------------~-------~--~----~
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-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: cache problem of new forms: make
ChoicesField's choices list on fly |
  United States |
2007-04-23 10:23:59 |
On 23 Kwi, 15:44, hoamon <hoa... gmail.com> wrote:
> Hi there~
>
> I have a ChoiceField() of [newforms] in CancelForm,
> it's choices list is depending on database table
sign2.
> my source code is below.
>
> --
> from django import newforms as forms
> class CancelForm(forms.Form):
> U_hash = {}
> for U in Sign2.objects.all():
> U_hash[U.unit.id] = U.unit.name
>
> Units = [('', '')]
> for (key, value) in U_hash.items():
> unit = (key, value)
> Units.append(unit)
>
> name = forms.CharField(label="name",
max_length=10)
> no = forms.CharField(label="personalid",
max_length=10)
> unit =
forms.ChoiceField(label="unitname",
choices=Units)
> --
>
> The problem is:
> When start apache server, the sign2 table only have two
unit's
> records. after some minutes, may be the unit's records
grows, but the
> CancelForm only have two choices. I must restart
apache server to
> renew the CancelForm.
>
> thanks for your patience.
You may want to look at ModelChoiceField(), look at:
http://code.djangoproject.com/browse
r/django/trunk/django/newforms/models.py
--
Robert
--~--~---------~--~----~------------~-------~--~----~
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-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: cache problem of new forms: make
ChoicesField's choices list on fly |
  United States |
2007-04-23 14:33:00 |
I think the problem you have is that the code assigning
choices is
only run the first time the class is loaded, unlike php
where it would
be read and loaded at each request, it only gets loaded once
here. To
get around this you need to move your dynamic code to the
forms
__init__ method so it runs each time an _instance_ is
created rather
than only once when the _class_ is created (the first time
mod_python
loads the file).
Something like this (not tested, but might be close)
class CancelForm(forms.Form):
name = forms.CharField(label="name",
max_length=10)
no = forms.CharField(label="personalid",
max_length=10)
unit = forms.ChoiceField(label="unitname",
choices=[])
def __init__(self,*args,*kwargs): # This will get run
every time a
form _instance_ is created
super(CancelForm, self).__init__(*args, **kwargs) #
This calls
the normal init which creates self.fields, etc
self.fields['unit'].widget.choices = [('',
'------------')] +
[(unit.id, unit.name) for unit in Sign2.objects.all()]
#override the
empty choices
--~--~---------~--~----~------------~-------~--~----~
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-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: cache problem of new forms: make
ChoicesField's choices list on fly |
  United States |
2007-04-23 14:36:39 |
Sorry, first post had an error.
class CancelForm(forms.Form):
name = forms.CharField(label="name",
max_length=10)
no = forms.CharField(label="personalid",
max_length=10)
unit = forms.ChoiceField(label="unitname",
choices=[])
def __init__(self,*args,**kwargs):
super(CancelForm, self).__init__(*args, **kwargs)
self.fields['unit'].widget.choices = [('',
'------------')] +
[(unit.id, unit.name) for unit in
Sign2.objects.all()]
--~--~---------~--~----~------------~-------~--~----~
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-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
[1-4]
|
|