I have a select box which has its options from a model called "Event". On my production server the result gets chached.
I need on every request the newest version of all Events in this select box. I asked in #django and someone said I should use lambda. I tried this with lambda but it doesn't work. Still get old values when I add a new Event only an apache restart show me the newest version.
Is there something wrong with my code?
#forms.py
events = lambda : [(e.id, e.title) for e in Event.objects.all().order_开发者_运维问答by('-date')]
class EventForm(Form):
event_title = ChoiceField(label='Veranstaltung', choices=events())
Grrr... comment boxes give me very little room for editing. I'll try it here:
The workaround is to use the __init__
of the form, i.e.
class EventForm(Form):
event_title = ChoiceField(label='Veranstaltung', choices=[])
def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args, **kwargs)
self.fields['events'].choices = [(e.id, e.title) for e in Event.objects.all().order_by('-date')]
By the way, did you consider using a ModelChoiceField
?
THIS ANSWER WILL NOT WORK, see comments
You're evaluating the lambda, rendering it useless.
Drop the brackets like so:
event_title = ChoiceField(label='Veranstaltung', choices=events)
Good luck!
精彩评论