4

独自のchoicefieldの選択肢を自動的に生成するフォーム(forms.Form)があります:

class UserForm(forms.Form):
    def generate_choices():
        from vn.account.models import UserProfile
        up = UserProfile.objects.filter(user__isnull=True)
        choices = [('0','--')]
        choices += ([(s.id ,'%s %s (%s), username: %s, email: %s' % (s.first_name, s.last_name, s.company_name, s.username, s.email)) for s in up])
        return choices

    user = forms.ChoiceField(label=_('Select from interest form'), choices=generate_choices())

私の問題は、これが(意図したとおりに)選択ボックスとして表示されますが、その内容が何らかの形でキャッシュされることです。ローカル PC で開発サーバーを再起動するか、リモート サーバーで Apache を再起動する前に、新しいエントリが表示されません。

そのコードはいつ評価されますか? 毎回エントリを再計算するようにするにはどうすればよいですか?

PS。memchached およびその他の種類のキャッシュはオフになっています。

4

2 に答える 2

2

フォームが呼び出されたときに評価されるように、initを介してこれを行う必要があると思います。

例えば

def __init__(self, *args, **kwargs):
    super(UserForm, self).__init__(*args, **kwargs)
    from vn.account.models import UserProfile
    up = UserProfile.objects.filter(user__isnull=True)
    choices = [('0','--')]
    choices += ([(s.id ,'%s %s (%s), username: %s, email: %s' % (s.first_name, s.last_name,s.company_name, s.username, s.email)) for s in up])

    self.fields['user'].choices = choices
于 2011-05-12T13:47:08.977 に答える