Opened 5 years ago

Closed 4 years ago

#12996 closed Bug (fixed) do not handle help_text correctly

Reported by: phretor Owned by: nobody
Component: Forms Version: master
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:


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 5 years ago.
Diff for ticket #12996
12996.2.diff (890 bytes) - added by jadinvt 5 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 5 years ago by russellm

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

  • Owner changed from nobody to jad
  • Status changed from new to assigned

Changed 5 years ago by jad

Diff for ticket #12996

comment:4 Changed 5 years ago by jad

  • Component changed from Uncategorized to Forms
  • Has patch set
  • Resolution set to fixed
  • Status changed from assigned to closed

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

  • Resolution fixed deleted
  • Status changed from closed to reopened

comment:6 Changed 5 years ago by jad

  • Owner changed from jad to nobody
  • Status changed from reopened to new

comment:7 Changed 5 years ago by jad

  • Triage Stage changed from Accepted to Ready for checkin

Changed 5 years ago by jadinvt

comment:8 Changed 4 years ago by Honza_Kral

  • Needs tests set
  • Triage Stage changed from Ready for checkin to Accepted

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

comment:9 Changed 4 years ago by lukeplant

  • Type set to Bug

comment:10 Changed 4 years ago by lukeplant

  • Severity set to Normal

comment:11 Changed 4 years ago by claudep

  • Easy pickings unset
  • Resolution set to fixed
  • Status changed from new to closed

This has been fixed in [16146]

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