#5803: class variables versus instance variables for Form (in particular choices
in ChoiceField)
-----------------------+----------------------------------------------------
Reporter: volov | Owner: nobody
Status: new | Component: django.newforms
Version: SVN | Keywords: ChoiceField choices share instance
Stage: Unreviewed | Has_patch: 0
-----------------------+----------------------------------------------------
{{{
>>> 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
:
[[BR]]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.
[[BR]]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.
--
Ticket URL: <http://code.djangoproject.com/ticket/5803>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---