Opened 17 years ago
Closed 17 years ago
#5803 closed (duplicate)
class variables versus instance variables for Form (in particular choices in ChoiceField)
Reported by: | volov | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | ChoiceField choices share instance | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
>>> from django import newforms as forms >>> class A(forms.Form): ... name=forms.CharField() ... books=forms.ChoiceField(choices=()) ... >>> a1=A() >>> a2=A() >>> a1.fields['name'].label="name1" >>> a2.fields['name'].label="name2" >>> str(a1) '<tr><th><label for="id_name">name1:</label></th><td><input type="text" name="name" id="id_name" /></td></tr>\n <tr><th><label for="id_books">Books:</label></th><td><select name="books" id="id_books">\n</select></td></tr>' >>> str(a2) '<tr><th><label for="id_name">name2:</label></th><td><input type="text" name="name" id="id_name" /></td></tr>\n <tr><th><label for="id_books">Books:</label></th><td><select name="books" id="id_books">\n</select></td></tr>' >>> a1.fields['books'].choices=[(1,'1'),(2,'2'),(3,'3')] >>> str(a1) '<tr><th><label for="id_name">name1:</label></th><td><input type="text" name="name" id="id_name" /></td></tr>\n <tr><th><label for="id_books">Books:</label></th><td><select name="books" id="id_books">\n <option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n</select></td></tr>' >>> str(a2) '<tr><th><label for="id_name">name2:</label></th><td><input type="text" name="name" id="id_name" /></td></tr>\n <tr><th><label for="id_books">Books:</label></th><td><select name="books" id="id_books">\n <option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n</select></td></tr>' >>>
here instance a2 has same ChoiceField? data as a1. (i need to display two formulars with differents choices value in a same page). why need to create two class instead of creating two instances ? Fields definition could be class variables but formular could be more reusable if fields data configuration are instance variable. For example, label name for fields are not shared then it should be the same for choices or do not allow to configure labels. it depends on what a formular object represents :
option 1) only "kind_ of" fields with standard behavior (a value of ChoiceField? should be on among choices value, email should respect specific format, and so on). you are html closer with this definition.
option 2) a formular is a set of fields with static preconfigure datas (choices for ChoiceField?) but you can change some preconfigure data : labels for example. In my case, i have to define two differents classes with exactly same definition :
>>> class A(forms.Form): ... name=forms.CharField() ... books=forms.ChoiceField(choices=()) ... >>> class B(forms.Form): ... name=forms.CharField() ... books=forms.ChoiceField(choices=()) ...
and then set differents choices :
>>> a1.fields['books'].choices=[(1,'1'),(2,'2'),(3,'3')] >>> a2.fields['books'].choices=[(2,'2'),(5,'5'),(21,'21')]
as django teams like DRY principle (see permalink doc), it seems better to choose option 1) as it avoids two same classes definition.
what is your opinion ?
regards,
volov.
Change History (2)
comment:1 by , 17 years ago
comment:2 by , 17 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Per note from submitter this was a mistaken re-post of #5800.