def __init__(self, *args, **kwargs): regiones = Ubigeo.objects.filter(parent__isnull=True).order_by('name') if 'ubigeo' in kwargs: if kwargs['ubigeo'] == constant.ONLY_INTERNATIONAL: regiones = regiones.filter( ubigeo__startswith='9').order_by('name') elif kwargs['ubigeo'] == constant.ONLY_PERU: regiones = regiones.exclude(ubigeo__startswith='9') provincias = Ubigeo.objects.filter(parent=regiones[0]).order_by('name') distritos = Ubigeo.objects.filter( parent=provincias[0]).order_by('name') self.fields = ( ModelChoiceField(queryset=regiones), ModelChoiceField(queryset=provincias), ModelChoiceField(queryset=distritos), ) self.widget = UbigeoWidget(regiones=self.fields[0]._get_choices(), provincias=self.fields[1]._get_choices(), distritos=self.fields[2]._get_choices()) super(UbigeoFormField, self).__init__(self.fields, self.widget, *args)