# todo : get a more descriptive FC prefix than "jpic"
class JpicModelForm(forms.ModelForm):
    """
    Inherit from this, then use this new keyword argument
    'jpic_field_options' with a dict of dicts such as:
    {                             
        'field_name_that_is_m2m': {
            'queryset': aqueryset,
        },
        'field_name_that_is_fk': {
            'queryset': anotherqueryset,
        },
    }

    This solves the problem of using a queryset made in the view
    as choices for a M2M/FK field ...
    """
    def __init__(self, *args, **kwargs):
        if 'jpic_field_options' in kwargs.keys():
            jpic_field_options = kwargs.pop('jpic_field_options')
        super(JpicModelForm, self).__init__(*args, **kwargs)
        for field, args in jpic_field_options.items():
            if field not in self.fields:
                continue 
            for arg, value in args.items():
                setattr(self.fields[field], arg, value)