3

I have a couple models lets call them Model A and Model B. There is a foreign key to A from B.
That is the cardinality between A and B is 1:n.

I have made a corresponding ModelForm for B called MF_B. I have an additional field I define in B. e.g.

class MF_B(forms.ModelForm):

    stuff = forms.MultipleChoiceField(queryset=None, required=False)

    class Meta:
        model=B

as the code above implies, I want to populate the choices here with a queryset. (i've omitted the override code in init() that we would use to set the queryset on the stuff field)

The deal is I want to use inlineformset_factory to create on one page a form with A and several forms with B.

I wanted to just pass a bunch of new kwargs to inlineformset_factory but it kept complaing about unexpected keyword arguments and after looking at the backend code in django, I understand why: inlineformset_factory doesn't take custom kwargs. it only takes kwargs defined in the method sig.

def inlineformset_factory(parent_model, model, form=ModelForm,
                          formset=BaseInlineFormSet, fk_name=None,
                          fields=None, exclude=None,
                          extra=3, can_order=False, can_delete=True, max_num=None,
                          formfield_callback=None):

I don't think ican use formfield_callback because i need to pass self.request.user to filter objects that come back in the queryset.

any suggestions?

w--
  • 6,427
  • 12
  • 54
  • 92
  • This was answered [here](http://stackoverflow.com/questions/7310861/django-passing-parameters-to-inline-formset) including an example that uses request.user to filter the inline forms' fields. – dgel Apr 17 '12 at 19:36
  • @dgel am using django 1.3 and class-based views. Will i still be able to access self.request somehow? Am trying it now but in case you know ahead of time. – w-- Apr 17 '12 at 20:41
  • Haven't actually used class-based views. As long as you create the inlineformset_factory and pass it the formfield_callback somewhere request.user is visible, you should be alright. – dgel Apr 17 '12 at 20:45
  • err.. formfield_callback doesn't execute on custom fields in a modelForm =\. Maybe you can tell me if i'm crazy? django/forms/models.py -> def fields_for_model -> line 146 – w-- Apr 17 '12 at 22:18
  • Why wouldn't custom fields be included in `_meta.fields`? – dgel Apr 17 '12 at 22:23
  • look at the definition for fields_for_model. no where do we pass the custom ModelForm object we specify in our call to inlineformset_factory. we only pass the model from our ModelForm definition. I confirmed by tracing the output to a file. the only fields in model._meta.fields are those in the actual model. =\ – w-- Apr 17 '12 at 22:28
  • I see. Looks like maybe you'll have to try something else. – dgel Apr 17 '12 at 22:30
  • =(. Thanks for the original suggestion and following this through with me. – w-- Apr 17 '12 at 22:31

1 Answers1

0

In order to restrict the MultipleChoiceField to a queryset you specify you have to use a factory. In this case it would be a inlineformset_factory_factory which is a little confusing, but for example:

in a forms.py (or similar)

def make_inlineformset_factory(queryset, xmodel):
    """
    Returns an Inlineformset factory for the given queryset . . .
    """
    class My_inlineformset_factory():
        stuff = forms.ModelChoiceField(queryset=queryset)
        class Meta:
            model = xmodel
    return My_inlineformset_factory

Specifically I am thinking an approach similar to the first answer here: django: How to limit field choices in formset? but then adapt it for the Inlineformset_factory appropriately.

I may edit to add more detail and description when I can find the time later.

Community
  • 1
  • 1
Jay H.
  • 843
  • 7
  • 6