When you explicitly write list in choices it evaluates at module
import type. To avoid this write function and pass it as 'choices'
parameter

def get_choices():
    [(d.id,d.name) for d in Deck.objects.all()]
#...
deck = forms.ChoiceField(required=True,choices = get_choices )

When this function will be called every time the form is rendered


On 13 янв, 21:00, Julian <[EMAIL PROTECTED]> wrote:
> Hi there,
>
> I have cards and decks, decks have ManyToMany cards, and if you want
> to add a card, you have to chose a deck; quite simple.
>
> my forms.py contains something like that:
>
> class CardForm(forms.Form):
>     deck = forms.ChoiceField(required=True,choices=[(d.id,d.name) for
> d in Deck.objects.all()])
>     question = forms.CharField(required=True,max_length=10000)
>     answer = forms.CharField(required=True,max_length=10000)
>
> obviously this doesn't work if i add a new deck, it doesn't appear in
> the select-list of the deck-form-field.
>
> Correct me if i'm wrong: the CardForm is not rendered each time I
> import it but each time i change the forms.py file.
>
> how do i avoid this?
>
> my solution is to create the list with the choices in the view...but i
> have to do it here and there, so i could save some lines of code.
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to