Skip to content

Instantly share code, notes, and snippets.

@vbmendes
Created February 24, 2011 20:55
Show Gist options
  • Save vbmendes/842863 to your computer and use it in GitHub Desktop.
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.
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)
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