开发者

get an error while applying a widget to a field

Here is my model

class RecipeIngredient(models.Model):
    recipe = models.ForeignKey(Recipe)
    ingredient = models.Foreign开发者_如何学GoKey(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?


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.


I ran into a similar problem.

I specified a custom "form" kwarg in a modelformset_factory constructor. This would consistently fail with the error you describe while that form class had custom widgets specified in its meta class:

class MyForm(forms.ModelForm):
  class Meta:
    model = MyModel
    widgets = {
        'myField' : forms.fields.TextInput(attrs={'readonly':'readonly'}),
    }

MyFormFactory = modelformset_factory(MyModel,form=MyForm)

Changing the form class to specify custom widgets in the init method seemed to solve this:

class MyForm(forms.ModelForm):
  class Meta:
    model = MyModel
  def __init__(self,*args,**kwargs):
    self.fields['myField'].widget = forms.fields.TextInput(attrs={'readonly':'readonly'})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜