Opened 13 years ago

Closed 12 years ago

#12996 closed Bug (fixed) 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


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 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 }
        return super(ITSocialSecurityNumberModelField, self).formfield(**defaults)

    def __init__(self, *args, **kwargs):
        defaults = { 'max_length': 16 }
        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:

File "/Users/phretor/ve/edilges/src/django/django/core/handlers/" in get_response
  80.                     response = middleware_method(request)
File "/Users/phretor/ve/edilges/src/django/django/middleware/" in process_request
  56.             if (not _is_valid_path(request.path_info) and
File "/Users/phretor/ve/edilges/src/django/django/middleware/" in _is_valid_path
  142.         urlresolvers.resolve(path)
File "/Users/phretor/ve/edilges/src/django/django/core/" in resolve
  307.     return get_resolver(urlconf).resolve(path)
File "/Users/phretor/ve/edilges/src/django/django/core/" in resolve
  220.             for pattern in self.url_patterns:
File "/Users/phretor/ve/edilges/src/django/django/core/" in _get_url_patterns
  249.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/phretor/ve/edilges/src/django/django/core/" in _get_urlconf_module
  244.             self._urlconf_module = import_module(self.urlconf_name)
File "/Users/phretor/ve/edilges/src/django/django/utils/" in import_module
  35.     __import__(name)
File "/Users/phretor/ve/edilges/src/django-edilges-tip/edilges/" in <module>
  5. admin.autodiscover()
File "/Users/phretor/ve/edilges/src/django/django/contrib/admin/" in autodiscover
  57.         import_module("%s.admin" % app)
File "/Users/phretor/ve/edilges/src/django/django/utils/" in import_module
  35.     __import__(name)
File "/Users/phretor/ve/edilges/src/django-cantiere-tip/cantiere/" in <module>
  21., DipendenteAdmin)
File "/Users/phretor/ve/edilges/src/django/django/contrib/admin/" in register
  94.             validate(admin_class, model)
File "/Users/phretor/ve/edilges/src/django/django/contrib/admin/" in validate
  23.     validate_base(cls, model)
File "/Users/phretor/ve/edilges/src/django/django/contrib/admin/" in validate_base
  213.             check_formfield(cls, model, opts, 'fields', field)
File "/Users/phretor/ve/edilges/src/django/django/contrib/admin/" in check_formfield
  340.         fields = fields_for_model(model)
File "/Users/phretor/ve/edilges/src/django/django/forms/" in fields_for_model
  186.         formfield = formfield_callback(f, **kwargs)
File "/Users/phretor/ve/edilges/src/django/django/forms/" 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/" in formfield
  33.         return super(ITProvinceSelectModelField, self).formfield(**defaults)
File "/Users/phretor/ve/edilges/src/django/django/db/models/fields/" in formfield
  581.         return super(CharField, self).formfield(**defaults)
File "/Users/phretor/ve/edilges/src/django/django/db/models/fields/" 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 13 years ago.
Diff for ticket #12996
12996.2.diff (890 bytes) - added by jadinvt 13 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 13 years ago by Russell Keith-Magee

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 Changed 13 years ago by Russell Keith-Magee

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 Changed 13 years ago by jad

Owner: changed from nobody to jad
Status: newassigned

Changed 13 years ago by jad

Attachment: 12996.diff added

Diff for ticket #12996

comment:4 Changed 13 years ago by jad

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 Changed 13 years ago by jad

Resolution: fixed
Status: closedreopened

comment:6 Changed 13 years ago by jad

Owner: changed from jad to nobody
Status: reopenednew

comment:7 Changed 13 years ago by jad

Triage Stage: AcceptedReady for checkin

Changed 13 years ago by jadinvt

Attachment: 12996.2.diff added

comment:8 Changed 13 years ago by Honza Král

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 Changed 12 years ago by Luke Plant

Type: Bug

comment:10 Changed 12 years ago by Luke Plant

Severity: Normal

comment:11 Changed 12 years ago by Claude Paroz

Easy pickings: unset
Resolution: fixed
Status: newclosed

This has been fixed in [16146]

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