# 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)
Comments
I was asked how this could be useful when you could do like: http://collingrady.wordpress.com/2008/07/24/useful-form-tricks-in-django/
It's useful to me because then all forms init() method can keep the same signature, making it usable in a factory.
#