How can I limit MultipleChoiceField's showing Items?
I have a MultipleChoiceField. I need to assign a integer value which provides that MultipleChoiceField shows only this number of element on the website? How can I do that?
Another question is where can I find any documentation whi开发者_高级运维ch contains answers of these question? I need a document like QT Asistant.
You can modify the choices of any choice field on init.
Consider this
class BookForm(forms.Form):
book = forms.ModelChoiceField(queryset=Book.objects.all())
user = forms.EmailField()
def __init__(self, author=None, *args, **kwargs):
super(CheckoutForm, self).__init__(*args, **kwargs)
if author:
self.fields['book'].queryset = Book.objects.filter(author__icontains=author)
In this example, you could call BookForm() like normal, or you could call BookForm(author="Tolkien")
That's how you would modify ModelChoices on the fly, you could see, it's the same process to modify normal choices on the fly as well, just pass an iterable (usually of 2-tuples)
精彩评论