Opened 17 years ago

Closed 17 years ago

#5800 closed (duplicate)

could not have several forms instance in a single page without sharing choicefields data

Reported by: volov2004@… Owned by: nobody
Component: Forms Version: 0.96
Severity: Keywords: ChoiceField isolation 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 (1)

comment:1 by Karen Tracey <kmtracey@…>, 17 years ago

Resolution: duplicate
Status: newclosed

This has the same root problem as #5665. Trunk no longer exhibits this behavior:

>>> from django import newforms as forms
>>> class A(forms.Form):
...      name=forms.CharField()
...      books=forms.ChoiceField(choices=())
...
>>> a1=A()
>>> a2=A()
>>> str(a1)
'<tr><th><label for="id_name">Name:</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">Name:</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">Name:</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">Name:</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>'
>>>

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