Opened 12 years ago

Closed 12 years ago

#19138 closed Bug (fixed)

Failed to pull initial data while Creating ModelForm instance with a prefix

Reported by: Charlie.Tang@… Owned by: nobody
Component: Forms Version: 1.4
Severity: Normal Keywords: ModelForm instance prefix form.data html_name name
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

ModelForm 's initial with a prefix failed to pull initial data

Example

class Project(models.Model):
    name = models.CharField(max_length=64,)

class Document(models.Model):
    project = models.ForeignKey(Project, )

class DocumentForm(ModelForm):
    class Meta:
        model = Document

The following with prefix='document' failed to have the initial selected in the dropdown list:

    form = DocumentForm(data={'project': a_Project_instance}, prefix='document')

But the following worked fine:

    form = DocumentForm(data={'project': a_Project_instance}, prefix='document')


fix:

diff -r c04f07d5be28 django/forms/forms.py
--- a/django/forms/forms.py	Tue Oct 02 15:49:55 2012 -0400
+++ b/django/forms/forms.py	Wed Oct 17 10:32:33 2012 -0400
@@ -477,7 +477,7 @@
         """
         Returns the data for this BoundField, or None if it wasn't given.
         """
-        return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name)
+        return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.name)
     data = property(_data)
 
     def value(self):

Problem: the keys of self.form.data is model's field_name, self.html_name has a prefix if the form's prefix is not None

Attachments (1)

patch-forms.py-c04f07d5be28.txt (536 bytes ) - added by Charlie Tang <Charlie.Tang@…> 12 years ago.

Download all attachments as: .zip

Change History (4)

by Charlie Tang <Charlie.Tang@…>, 12 years ago

comment:1 by Charlie Tang <Charlie.Tang@…>, 12 years ago

ModelForm 's initial with a prefix failed to pull initial data

Example

class Project(models.Model):
    name = models.CharField(max_length=64,)

class Document(audit.models.AuditModel):
    project = models.ForeignKey(Project, )

class DocumentForm(ModelForm):
    class Meta:
        model = Document

The following with prefix='document' failed to have the initial selected in the dropdown list:

    form = DocumentForm(data={'project': a_Project_instance}, prefix='document')

But the following worked fine:

    form = DocumentForm(data={'project': a_Project_instance},)

comment:2 by Charlie Tang <Charlie.Tang@…>, 12 years ago

Don't need fix.

the data dictionary's key need to have a prefix

such as:

form = DocumentForm(data={'document-project': a_Project_instance}, prefix='document')

comment:3 by Charlie Tang <Charlie.Tang@…>, 12 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top