views:

29

answers:

1

Here is my model

class RecipeIngredient(models.Model):
    recipe = models.ForeignKey(Recipe)
    ingredient = models.ForeignKey(Ingredient)
    serving_size = models.ForeignKey(ServingSize)
    quantity = models.IntegerField()
    order = models.IntegerField()
    created = models.DateTimeField(auto_now_add = True)
    updated = models.DateTimeField(auto_now = True)

and my model form

class RecipeIngredientForm(forms.ModelForm):
    recipe_ingredient = forms.CharField()

    class Meta:
        model = RecipeIngredient

        widgets = {
            'serving_size' : forms.Select(attrs={'class' : 'test'}), 
            'recipe_ingredient' : forms.TextInput(),
        }

I get the following error when I navigate to the page

Unhandled exception in thread started by <function inner_run at 0x1010faf50>
Traceback (most recent call last):
  File "/Library/Python/2.6/site-packages/django/core/management/commands/runserver.py", line 48, in inner_run
    self.validate(display_num_errors=True)
  File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 245, in validate
    num_errors = get_validation_errors(s, app)
  File "/Library/Python/2.6/site-packages/django/core/management/validation.py", line 28, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/Library/Python/2.6/site-packages/django/db/models/loading.py", line 146, in get_app_errors
    self._populate()
  File "/Library/Python/2.6/site-packages/django/db/models/loading.py", line 61, in _populate
    self.load_app(app_name, True)
  File "/Library/Python/2.6/site-packages/django/db/models/loading.py", line 78, in load_app
    models = import_module('.models', app_name)
  File "/Library/Python/2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/models.py", line 128, in <module>
    RecipeIngredientFormSet = inlineformset_factory(Recipe, RecipeIngredient, extra=1, form=RecipeIngredientForm)
  File "/Library/Python/2.6/site-packages/django/forms/models.py", line 838, in inlineformset_factory
    FormSet = modelformset_factory(model, **kwargs)
  File "/Library/Python/2.6/site-packages/django/forms/models.py", line 669, in modelformset_factory
    formfield_callback=formfield_callback)
  File "/Library/Python/2.6/site-packages/django/forms/models.py", line 407, in modelform_factory
    return ModelFormMetaclass(class_name, (form,), form_class_attrs)
  File "/Library/Python/2.6/site-packages/django/forms/models.py", line 220, in __new__
    opts.exclude, opts.widgets, formfield_callback)
  File "/Library/Python/2.6/site-packages/django/forms/models.py", line 178, in fields_for_model
    formfield = formfield_callback(f, **kwargs)
TypeError: <lambda>() got an unexpected keyword argument 'widget'

but if I remove this line from my RecipeIngredientForm the error goes away

'serving_size' : forms.Select(attrs={'class' : 'test'}), 

Any ideas whatI did wrong?

A: 

Just don't add Select widget to this field, it's ModelChoiceField, and is rendered as select box anyway. If you put Select widget on ModelChoiceField, it won't render properly (will show unicode output in value rather than ID).

Also you don't need the first CharField unless you want a text input with auto-completion.

To show the input element with custom attributes, use {% field %} tag:

{% field myform.myselect class="XYZ" %}

renders it to

<select name="..." class="XYZ">...

I think you'll agree that it's a bad idea to customize classes and templates inside forms or models. It makes it impossible to debug templates.

culebrón
Thanks for the reply. This is actually a bug in djano (http://code.djangoproject.com/ticket/13095). If you can clarify some of the things you mentioned. You said that don't add the select widget, but if I wanted to add a class attributes, how would I do it? The select field is rendering properly, the issue was with the custom field in the custom modelform class.
iHeartDucks
I've edited the answer to include what you ask.
culebrón