Django

Code

Ticket #6953 (new)

Opened 2 years ago

Last modified 1 year ago

ModelForm as_FOO output places form fields for ManyToMany fields at bottom of html output.

Reported by: brooks.travis@gmail.com Assigned to: Alex
Milestone: Component: Forms
Version: SVN Keywords:
Cc: django@worksology.com, dgouldin@gmail.com Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

I have a ModelForm? based on a model with two ManyToMany? fields. Whenever I output the form using the as_FOO convenience methods, the ModelMultipleChoiceField? widgets are placed at the end of the ouput, rather than at the top of the output, a la their position in the model.

Attachments

6953.diff (7.0 kB) - added by dgouldin on 09/11/08 10:21:15.
modelform-m2m.diff (7.4 kB) - added by Alex on 02/22/09 10:50:53.

Change History

05/14/08 13:25:45 changed by anonymous

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

05/14/08 13:48:55 changed by brooks.travis@gmail.com

Example Model:

class SupportTicket(models.Model):
	client = models.ManyToManyField(Client)
	call_type = models.ManyToManyField("CallType")
	priority = models.CharField(choices=PRIORITY_LEVELS, default=3, max_length=2)
	call_source = models.ForeignKey("CallSource", null=True, blank=True)
	call_description = models.TextField("Call Description", max_length=500)
	call_status = models.ForeignKey("CallStatus")
	cause = models.ForeignKey("Causes", null=True, blank=True)
	equipment_tag = models.CharField(max_length=50, null=True, blank=True)
	solution = models.TextField(max_length=500, blank=True)
	date_created = models.DateTimeField(default=datetime.datetime.now, editable=False)
	created_by = models.ForeignKey(User, related_name='ticket_created')
	date_modified = models.DateTimeField(null=True, blank=True, editable=False)
	last_modified_by = models.ForeignKey(User, related_name='ticket_modified', null=True, blank=True)
	target_date = models.DateTimeField(null=True, blank=True)
	date_closed = models.DateTimeField(null=True, blank=True)
	closed_by = models.ForeignKey(User, related_name='ticket_closed', null=True, blank=True)
	
	def __unicode__(self):
		return u"%s" % self.id
	
	def save(self):
		if self.id:
			self.date_modified = datetime.datetime.now()
		super(SupportTicket, self).save()

Example ModelForm?:

class TicketForm(forms.ModelForm):

	class Meta:
		model = SupportTicket
		exclude = ('date_created', 'created_by', 'date_modified', 'last_modified_by', 'date_closed', 'closed_by')

Example as_FOO output:

<p><label for="id_priority">Priority:</label> <select name="priority" id="id_priority">
<option value="4">LOW</option>
<option value="3" selected="selected">MEDIUM</option>
<option value="2">HIGH</option>
<option value="1">EMERGENCY</option>
</select></p>
<p><label for="id_call_source">Call source:</label> <select name="call_source" id="id_call_source">
<option value="" selected="selected">---------</option>
<option value="2">Phone</option>
</select></p>
<p><label for="id_call_description">Call Description:</label> <textarea id="id_call_description" rows="10" cols="40" name="call_description"></textarea></p>
<p><label for="id_call_status">Call status:</label> <select name="call_status" id="id_call_status">
<option value="" selected="selected">---------</option>
<option value="1">In Progress</option>
</select></p>
<p><label for="id_cause">Cause:</label> <select name="cause" id="id_cause">
<option value="" selected="selected">---------</option>
</select></p>
<p><label for="id_equipment_tag">Equipment tag:</label> <input id="id_equipment_tag" type="text" name="equipment_tag" maxlength="50" /></p>
<p><label for="id_solution">Solution:</label> <textarea id="id_solution" rows="10" cols="40" name="solution"></textarea></p>
<p><label for="id_target_date">Target date:</label> <input type="text" name="target_date" id="id_target_date" /></p>
<p><label for="id_client">Client:</label> <select multiple="multiple" name="client" id="id_client">
<option value="1">Brooks Travis</option>
</select>  Hold down "Control", or "Command" on a Mac, to select more than one.</p>
<p><label for="id_call_type">Call type:</label> <select multiple="multiple" name="call_type" id="id_call_type">
<option value="1">Hard Drive</option>
</select>  Hold down "Control", or "Command" on a Mac, to select more than one.</p>

05/14/08 13:49:32 changed by anonymous

  • version changed from SVN to newforms-admin.

06/09/08 17:07:05 changed by Karen Tracey <kmtracey@gmail.com>

  • version changed from newforms-admin to SVN.

What does this have to do with newforms-admin? It was originally opened with version SVN and anonymously changed to nfa. Reverting. If there's something nfa specific about this ticket please spell it out.

08/08/08 15:43:36 changed by ericholscher

  • stage changed from Unreviewed to Accepted.
  • milestone set to 1.0.

08/22/08 23:16:19 changed by ubernostrum

  • milestone changed from 1.0 to post-1.0.

As a sort of cosmetic change, I think this can safely go post-1.0.

09/11/08 10:21:15 changed by dgouldin

  • attachment 6953.diff added.

09/11/08 10:24:44 changed by dgouldin

  • has_patch set to 1.

02/22/09 10:50:53 changed by Alex

  • attachment modelform-m2m.diff added.

02/22/09 12:09:59 changed by worksology

  • cc set to django@worksology.com.

This patch definitely fixes the problem as I was experiencing it. If the patch is truly this simple, this should have made it into 1.0. This is NOT just a cosmetic defect. The order of fields in most forms is damn important, and if the only other option is to hardcode the fields in the right order in your templates, then this seems to be a significant issue.

Thanks for the patch Alex.

02/24/09 19:53:51 changed by Alex

  • owner changed from nobody to Alex.

02/25/09 13:51:44 changed by

  • milestone deleted.

Milestone post-1.0 deleted

03/16/09 08:46:29 changed by dgouldin

Alex, it looks like the sorted builtin was added in Python 2.4:

http://docs.python.org/library/functions.html#sorted

Are we interested in keeping 2.3 support as specified here?

http://docs.djangoproject.com/en/dev/topics/install/#install-python

03/16/09 08:48:53 changed by Alex

Yeah, I have a branch on github with my work on this and I added the whole conditional import of sorted thing for compatibility, it's the modelform-m2m branch :)

03/16/09 08:51:00 changed by dgouldin

  • cc changed from django@worksology.com to django@worksology.com, dgouldin@gmail.com.

Hah! Learn something new every day ...


Add/Change #6953 (ModelForm as_FOO output places form fields for ManyToMany fields at bottom of html output.)




Change Properties
Action