[Answer]-Passing a kwargs parameter to a ModelForm Field constructor?

1👍

If visible_things is a queryset, you can change the queryset attribute of the form field:

self.fields['things'].queryset = instance.visible_things

It really does have to be a queryset, not an arbitrary iterable, but other than that it’s easy.

0👍

Just add a kwarg if the arg is optional:

myform = ThingSettingsForm(thing=<my instance>)

You’ll have to change the init method to pop your new arg first, as super isn’t expecting this arg:

def __init__(self, *args, **kwargs):
    instance = kwargs.pop('thing', None)
    super(ThingSettingsForm, self).__init__(*args, **kwargs)

Or if its required, just add it into the init signature:

def __init__(self, thing, *args, **kwargs):
    pass

and call it thus:

myform = ThingSettingsForm(<my instance>)

Leave a comment