views:

126

answers:

1

Hi,

When I render my formset, one of the field renders as a select box because it is a foreign field in the model. Is there a way to change this to a text input? I want to populate that field by using Ajax auto complete. Adding a widget to the modelform is not working because the modelformset_factory takes a model and not a model form.

EDIT

My Model Form

class RecipeIngredientForm(ModelForm):
    class Meta:
        model = RecipeIngredient

        widgets = { 'ingredient' : TextInput(), }

I use it in my view

RecipeIngredientFormSet = modelformset_factory(RecipeIngredient, form=RecipeIngredientForm)
    objRecipeIngredients = RecipeIngredientFormSet()

EDITED MODEL FORM

class RecipeIngredientForm(ModelForm):
    ingredient2 = TextInput()
    class Meta:
        model = RecipeIngredient

I create the form set like this

RecipeIngredientFormSet = modelformset_factory(RecipeIngredient, form=RecipeIngredientForm)
    objRecipeIngredients = RecipeIngredientFormSet()

QUESTION

Do I have to use the formset in html? Can I just hard code the fields that get generated and using javascript I can create new fields and increment the "form-TOTAL-FORMS"? If I can then I do not have to worry about my model form.

Thanks

A: 

modelformset_factory does take a form. Here's the function signature from django.forms.models:

def modelformset_factory(
           model, form=ModelForm, formfield_callback=lambda f: f.formfield(),
           formset=BaseModelFormSet,
           extra=1, can_delete=False, can_order=False,
           max_num=0, fields=None, exclude=None):

If this isn't working for you, show some code and I'll try and see what is going wrong.

Edit after various comments As you point out, the widget argument is buggy when used in this way. So the solution is not to use it - it's a very recent addition in any case. Instead, define the field directly on the form:

class RecipeIngredientForm(forms.ModelForm):
    ingredient = forms.ModelChoiceField(widget=forms.TextInput))
    class Meta:
        model = RecipeIngredient
Daniel Roseman
I updated my code but I get an error which says "<lambda>() got an unexpected keyword argument 'widget'". I only pass two parameters to the function.
iHeartDucks
Hi Daniel, thanks for the reply. I am unable to render the field, I still see the model form field.
iHeartDucks