Code

Opened 7 years ago

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

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.

Attachments (0)

Change History (1)

comment:1 Changed 7 years ago by Karen Tracey <kmtracey@…>

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

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>'
>>>

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.