Code

Opened 4 years ago

Closed 4 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: UI/UX:

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.

Attachments (0)

Change History (7)

comment:1 follow-up: Changed 4 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Uhh, fields have max_length available to them.

comment:2 in reply to: ↑ 1 ; follow-up: Changed 4 years ago by anonymous

  • Resolution invalid deleted
  • Status changed from closed to 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 Changed 4 years ago by Alex

  • Resolution set to invalid
  • Status changed from reopened to closed

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

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

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. 
       """


comment:5 Changed 4 years ago by anonymous

form.title.field.field.max_length

does not work.

comment:6 follow-up: Changed 4 years ago by anonymous

  • Resolution invalid deleted
  • Status changed from closed to reopened

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

  • Resolution set to invalid
  • Status changed from reopened to closed

Okay {{{
form.title.field.max_length
}}} works.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.