from django.db.models.fields.related import ForeignKey
import form_fields
try:
    from south.modelsinspector import add_introspection_rules
    has_south = True
except:    
    has_south = False


class ChainedForeignKey(ForeignKey):
    """
    chains the choices of a previous combo box with this one
    """
    def __init__(self, to, chained_field, chained_model_field, *args, **kwargs):
        self.app_name = to._meta.app_label
        self.model_name = to._meta.object_name
        self.chain_field = chained_field
        self.model_field = chained_model_field
        ForeignKey.__init__(self, to, *args, **kwargs)

    def formfield(self, **kwargs):
        defaults = {
            'form_class': form_fields.ChainedModelChoiceField,
            'queryset': self.rel.to._default_manager.complex_filter(self.rel.limit_choices_to),
            'to_field_name': self.rel.field_name,
            'app_name': self.app_name,
            'model_name': self.model_name,
            'chain_field': self.chain_field,
            'model_field': self.model_field,
        }
        defaults.update(kwargs)
        return super(ChainedForeignKey, self).formfield(**defaults)


class GroupedForeignKey(ForeignKey):
    """
    Opt Grouped Field
    """
    def __init__(self, to, group_field, *args, **kwargs):
        self.group_field = group_field
        self._choices = True
        ForeignKey.__init__(self, to, *args, **kwargs)
    
    def formfield(self, **kwargs):
        defaults = {
            'form_class': form_fields.GroupedModelSelect,
            'queryset': self.rel.to._default_manager.complex_filter(
                                                    self.rel.limit_choices_to),
            'to_field_name': self.rel.field_name,
            'order_field': self.group_field,
        }
        defaults.update(kwargs)
        return super(ForeignKey, self).formfield(**defaults)
    
if has_south:
    add_introspection_rules([], ["^smart_selects\.db_fields\.ChainedForeignKey"])
    add_introspection_rules([], ["^smart_selects\.db_fields\.GroupedForeignKey"])
@@ -345,16 +345,21 @@
 ed_field
+=None
 , chaine
@@ -367,31 +367,29 @@
 _model_field
-, *args
+=None
 , **kwargs):
@@ -596,39 +596,32 @@
 init__(self, to,
- *args,
  **kwargs)%0A%0A    
@@ -1163,17 +1163,25 @@
 faults)%0A
+    %0A   
 %0A
-
 %0Aclass G
@@ -1287,24 +1287,16 @@
 p_field,
- *args, 
 **kwargs
@@ -1366,16 +1366,16 @@
  = True%0A
+
         
@@ -1407,15 +1407,8 @@
  to,
- *args,
  **k

make the fields run in south migrations