Opened 15 years ago
Closed 15 years ago
#13326 closed (invalid)
class ModelForm should copy Model 's inner class Field max_length attribute
Reported by: | 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 |
Description
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)
follow-up: 2 comment:1 by , 15 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
follow-up: 4 comment:2 by , 15 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
Replying to Alex:
xxx fields have
max_length
available to them.
Not in ModelForm. For example:
model.py
class Example(models.Model): """An Example""" title = models.CharField(blank=True, max_length=30)
example_form.html
<form action ="" method="POST" name="exampleForm" Title: {{ form.title }} <input type="submit" name="submit" value="Example" id="submit"> </form> <br> This is OKAY: {{ form.title.label }} : <br> This is NOT okay: {{ form.title.max_length }} <br>
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
attribute.
Question: Is this the intended behavior?
comment:3 by , 15 years ago
Resolution: | → invalid |
---|---|
Status: | reopened → closed |
It is available as form.title.field.field.max_length
.
comment:4 by , 15 years ago
incl: excerpts of /site-packages/django/forms/fields.py
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. """
follow-up: 7 comment:6 by , 15 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
comment:7 by , 15 years ago
Resolution: | → invalid |
---|---|
Status: | reopened → closed |
Okay {{{
form.title.field.max_length
}}} works.
Uhh, fields have
max_length
available to them.