Opened 15 years ago

Closed 14 years ago

#12996 closed Bug (fixed)

django.contrib.localflavor.it.forms do not handle help_text correctly

Reported by: phretor Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

A "init() got an unexpected keyword argument 'help_text'" exception is raised if the following custom model field is used. The problem arises whenever formfield() is called by a ModelForm subclasses.

from django.contrib.localflavor.it.forms import *
from django.db import models
from django.utils.translation import ugettext_lazy as _

class ITSocialSecurityNumberModelField(models.CharField):
    """
    The Italian "Codice Fiscale" (CF) model field.
    """

    def formfield(self, **kwargs):
        defaults = { 'form_class': ITSocialSecurityNumberField }
        defaults.update(kwargs)
        return super(ITSocialSecurityNumberModelField, self).formfield(**defaults)

    def __init__(self, *args, **kwargs):
        defaults = { 'max_length': 16 }
        defaults.update(kwargs)
        super(ITSocialSecurityNumberModelField, self).__init__(*args, **defaults)

The problem is that most of the IT localflavor form field do not handle help_text. Instead, they pass along args/kwargs. Here is an example traceback from my own code:

Traceback:
File "/Users/phretor/ve/edilges/src/django/django/core/handlers/base.py" in get_response
  80.                     response = middleware_method(request)
File "/Users/phretor/ve/edilges/src/django/django/middleware/common.py" in process_request
  56.             if (not _is_valid_path(request.path_info) and
File "/Users/phretor/ve/edilges/src/django/django/middleware/common.py" in _is_valid_path
  142.         urlresolvers.resolve(path)
File "/Users/phretor/ve/edilges/src/django/django/core/urlresolvers.py" in resolve
  307.     return get_resolver(urlconf).resolve(path)
File "/Users/phretor/ve/edilges/src/django/django/core/urlresolvers.py" in resolve
  220.             for pattern in self.url_patterns:
File "/Users/phretor/ve/edilges/src/django/django/core/urlresolvers.py" in _get_url_patterns
  249.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/phretor/ve/edilges/src/django/django/core/urlresolvers.py" in _get_urlconf_module
  244.             self._urlconf_module = import_module(self.urlconf_name)
File "/Users/phretor/ve/edilges/src/django/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/Users/phretor/ve/edilges/src/django-edilges-tip/edilges/urls.py" in <module>
  5. admin.autodiscover()
File "/Users/phretor/ve/edilges/src/django/django/contrib/admin/__init__.py" in autodiscover
  57.         import_module("%s.admin" % app)
File "/Users/phretor/ve/edilges/src/django/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/Users/phretor/ve/edilges/src/django-cantiere-tip/cantiere/admin.py" in <module>
  21. admin.site.register(Dipendente, DipendenteAdmin)
File "/Users/phretor/ve/edilges/src/django/django/contrib/admin/sites.py" in register
  94.             validate(admin_class, model)
File "/Users/phretor/ve/edilges/src/django/django/contrib/admin/validation.py" in validate
  23.     validate_base(cls, model)
File "/Users/phretor/ve/edilges/src/django/django/contrib/admin/validation.py" in validate_base
  213.             check_formfield(cls, model, opts, 'fields', field)
File "/Users/phretor/ve/edilges/src/django/django/contrib/admin/validation.py" in check_formfield
  340.         fields = fields_for_model(model)
File "/Users/phretor/ve/edilges/src/django/django/forms/models.py" in fields_for_model
  186.         formfield = formfield_callback(f, **kwargs)
File "/Users/phretor/ve/edilges/src/django/django/forms/models.py" in <lambda>
  162. def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
File "/Users/phretor/ve/edilges/src/django-profields-tip/profields/localflavor/it/fields.py" in formfield
  33.         return super(ITProvinceSelectModelField, self).formfield(**defaults)
File "/Users/phretor/ve/edilges/src/django/django/db/models/fields/__init__.py" in formfield
  581.         return super(CharField, self).formfield(**defaults)
File "/Users/phretor/ve/edilges/src/django/django/db/models/fields/__init__.py" in formfield
  468.         return form_class(**defaults)

Exception Type: TypeError at /admin
Exception Value: __init__() got an unexpected keyword argument 'help_text'

Attachments (2)

12996.diff (2.3 KB ) - added by jad 15 years ago.
Diff for ticket #12996
12996.2.diff (890 bytes ) - added by jadinvt 15 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 by Russell Keith-Magee, 15 years ago

Triage Stage: UnreviewedAccepted

You have given an example that talks about ITSocialSecurityNumberField, but then your stack trace talks about ITProvinceSelectModelField. I can't reproduce the any problem like the one you describe with the ITSocialSecurityNumberField and help_text.

I can, however, find a problem with max_length and min_length handling. I'll accept the ticket on that basis.

comment:2 by Russell Keith-Magee, 15 years ago

Clarifying the problem: ITSocialSecurityNumberField hard codes a value of None for min and max length. If you have a model field that is passing in a value for max_length to form field (which will happen if you use the sample model field provided in this report), you will raise errors due to duplicate max_length/min_length arguments.

comment:3 by jad, 15 years ago

Owner: changed from nobody to jad
Status: newassigned

by jad, 15 years ago

Attachment: 12996.diff added

Diff for ticket #12996

comment:4 by jad, 15 years ago

Component: UncategorizedForms
Has patch: set
Resolution: fixed
Status: assignedclosed

Changed init of ITSocialSecurityNumberField and ITZipCodeField to accept min_length, max_length, and error_message as parameters with None defaults. They are then properly passed along to the super. Previously, they were passed along to the super (potentially) as a kwarg as well as explicitely.

comment:5 by jad, 15 years ago

Resolution: fixed
Status: closedreopened

comment:6 by jad, 15 years ago

Owner: changed from jad to nobody
Status: reopenednew

comment:7 by jad, 15 years ago

Triage Stage: AcceptedReady for checkin

by jadinvt, 15 years ago

Attachment: 12996.2.diff added

comment:8 by Honza Král, 14 years ago

Needs tests: set
Triage Stage: Ready for checkinAccepted

Please provide the example in forms of tests, after that we can commit it. Thanks!

comment:9 by Luke Plant, 14 years ago

Type: Bug

comment:10 by Luke Plant, 14 years ago

Severity: Normal

comment:11 by Claude Paroz, 14 years ago

Easy pickings: unset
Resolution: fixed
Status: newclosed

This has been fixed in [16146]

Note: See TracTickets for help on using tickets.
Back to Top