Skip to content

Instantly share code, notes, and snippets.

@joaodubas
Forked from vbmendes/forms.py
Created March 1, 2011 00:52
Show Gist options
  • Save joaodubas/848394 to your computer and use it in GitHub Desktop.
Save joaodubas/848394 to your computer and use it in GitHub Desktop.
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