Opened 9 years ago

Closed 9 years ago

#13326 closed (invalid)

class ModelForm should copy Model 's inner class Field max_length attribute

Reported by: m2k1985@… Owned by: nobody
Component: Core (Other) Version: 1.1
Severity: Keywords: ModelForm
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When using model-based forms, when the framework create form class from the base model, max_length attribute of fields should be copied to the corresponding fields in ModelForm class.

This will allow template to use such attribute as variable tags, keeping consistency from model all the way to form. This is especially so when using django.views.generic.create_object, where the ModelForm is generated by the framework.

The current 1.1.1 implementation achieve this only when widget is used in the template.

However, if the template wants to use javascript UI such as dojo, the field attribute max_length cannot be accessed if model-based form is used.

Change History (7)

comment:1 Changed 9 years ago by Alex Gaynor

Resolution: invalid
Status: newclosed

Uhh, fields have max_length available to them.

comment:2 in reply to:  1 ; Changed 9 years ago by anonymous

Resolution: invalid
Status: closedreopened

Replying to Alex:

xxx fields have max_length available to them.

Not in ModelForm. For example:

class Example(models.Model): 
        """An Example""" 
        title = models.CharField(blank=True, max_length=30)


<form action ="" method="POST" name="exampleForm" 
Title: {{ form.title }} 
<input type="submit" name="submit" value="Example" id="submit"> 
This is OKAY:  {{ form.title.label }} : 
This is NOT okay: {{ form.title.max_length }} 

For create_update generic view, the framework create a ModelForm object 'form', but in package form, class Field only has the following instance attributes. In particular, why attribute label worked and others did not, can be explained.
In further tracing, why widget method worked is because the max_length attribute of the CharField of the model has a special widget method, which copies the max_length attribute into a 'attr' dictionary, which is in turn used by the widget's render method.

Conclusion is (as of Django 1.1.1): within a template, variable tags
can access fields, but not their attributes, except for the label
Question: Is this the intended behavior?

comment:3 Changed 9 years ago by Alex Gaynor

Resolution: invalid
Status: reopenedclosed

It is available as form.title.field.field.max_length .

comment:4 in reply to:  2 Changed 9 years ago by anonymous

incl: excerpts of /site-packages/django/forms/

class Field(object): 
    def __init__(self, required=True, widget=None, label=None, initial=None, 
                 help_text=None, error_messages=None, show_hidden_initial=False): 
       # required -- Boolean that specifies whether the field is required. 
        #             True by default. 
        # widget -- A Widget class, or instance of a Widget class, that should 
        #           be used for this Field when displaying it. Each Field has a 
        #           default Widget that it'll use if you don't specify this. In 
        #           most cases, the default widget is TextInput. 
        # label -- A verbose name for this field, for use in displaying this 
        #          field in a form. By default, Django will use a "pretty" 
        #          version of the form field name, if the Field is part of a 
        #          Form. 
        # initial -- A value to use in this Field's initial display. This value 
        #            is *not* used as a fallback if data isn't given. 
        # help_text -- An optional string to use as "help text" for this Field. 
        # show_hidden_initial -- Boolean that specifies if it is needed to render a 
        #                        hidden widget with initial value after widget. 

comment:5 Changed 9 years ago by anonymous


does not work.

comment:6 Changed 9 years ago by anonymous

Resolution: invalid
Status: closedreopened

comment:7 in reply to:  6 Changed 9 years ago by anonymous

Resolution: invalid
Status: reopenedclosed

Okay {{{
}}} works.

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