Opened 18 years ago
Closed 18 years ago
#4022 closed (invalid)
Problem with select fields and foreign keys
Reported by: | Owned by: | Jacob | |
---|---|---|---|
Component: | Uncategorized | Version: | 0.96 |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I posted a similar message to this on the django users list a couple days ago but go no response, so it's possible this is actually a bug (not just a user error :)).
The problem is with values not getting saved in select fields. Specifically, select fields that are referencing an external type.
Say I have a model such as:
class MyModel(models.Model):
field1 = models.ForeignKey(Foo)
field2 = models.CharField(choices = (('1', 'first choice'), ('2', 'second
choice')))
When I use a manipulator to build the form all goes smoothly. However, if I select a value for field1 on the form, but not field2 I'll get an error since field2 is required. But when this happens field1 gets reset, so the value isn't being saved. If I go the other way, and set field2, but not field1, I'll get an error message again but field2 will have its values preserved.
Looking into the django code a bit I narrowed down the issue to the django.oldforms.FormField.extract_data method. When it runs the line:
data = data_dict.get(self.get_member_name(), None)
The data for field1 won't be returned. I believe this is because the 'member_name' for field1 is field1_id, but the 'field_name' is field1.
Let me know if there's any other information I can provide.
- Scott
Change History (7)
comment:1 by , 18 years ago
comment:3 by , 18 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
Were you perhaps using a custom manipulator without a flatten_data
method? Could you post the code you were using in the view (please use {{{
and }}}
around your code so it gets formatted properly)? Without that information I am marking this ticket as worksforme.
comment:4 by , 18 years ago
Resolution: | worksforme |
---|---|
Status: | closed → reopened |
Ok, so I've made a very simple app to demonstrate the problem (no custom manipulators or anything). Hopefully my formatting will work this time :).
models.py
from django.db import models class Foo(models.Model): name = models.CharField(maxlength = 100) def __str__(self): return self.name class Admin: pass class Bar(models.Model): field1 = models.ForeignKey(Foo) field2 = models.CharField(maxlength = 1, choices = (('1', 'first choice'), ('2', 'second choice')))
views.py
from django.shortcuts import render_to_response from django import forms from myapp.formtest.models import Bar def create(request): errors = new_data = {} manipulator = Bar.AddManipulator() if request.POST: new_data = request.POST.copy() errors = manipulator.get_validation_errors(new_data) if not errors: manipulator.do_html2python(new_data) manipulator.save(new_data) request.session['message'] = 'Bar Created' return HttpResponseRedirect('/formtest') form = forms.FormWrapper(manipulator, new_data, errors) return render_to_response('formtest/bar_form.html', {'form': form})
And finally, my bar_form.html
{% if form.error_dict %} <p>Please correct the error below.</p> {% endif %} <form action="" method="post"> Field1: {{ form.field1 }}<br /> Field2: {{ form.field2 }}<br /> <input type="submit" value="Save" class="default" /> </form>
Both field1 and field2 are required. So if only one of the two fields is set on the form, it won't save and will return an error. If field2 is set, it remains set when the page returns with an error message. However, if field1 (the foreign key field) is set (but not field2) the page returns with an error, but with neither fields set. So field1 gets reset.
comment:5 by , 18 years ago
In your view code, try moving the manipulator.do_html2python(new_data)
line above the if not errors:
line (see NewAdminChanges). Does that fix the problem you are seeing? This was something that was wrong in the documentation for a long time.
I don't honestly know if there's a bug here, but please be advised that we are not really applying fixes to the oldforms system at this point, since it's deprecated and scheduled to be removed.