Created
February 24, 2011 20:55
-
-
Save vbmendes/842863 to your computer and use it in GitHub Desktop.
Cria o formulário com o campo de país sendo um ModelChoiceField e no __init__ altera a queryset para contemplar o continente que você passar como parâmetro. E na view apenas passa o continente como parâmetro para o form.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from models import Pais | |
class MyForm(forms.Form): | |
pais = forms.ModelChoiceField(queryset = Pais.objects.all()) | |
def __init__(self, *args, **kwargs): | |
continente = kwargs.pop('continente', None) | |
super(MyForm, self).__init__(*args, **kwargs) | |
if continente: | |
self.fields['pais'].queryset = self.fields['pais'].queryset.filter(continente=continente) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from forms import MyForm | |
def my_view(request): | |
... | |
form = MyForm(continente='americano') | |
... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment