Opened 13 years ago
Closed 13 years ago
#19138 closed Bug (fixed)
Failed to pull initial data while Creating ModelForm instance with a prefix
| Reported by: | 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)
Change History (4)
by , 13 years ago
| Attachment: | patch-forms.py-c04f07d5be28.txt added |
|---|
comment:1 by , 13 years ago
comment:2 by , 13 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 , 13 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
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 = DocumentThe 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},)