#4022: Problem with select fields and foreign keys
--------------------------------+-------------------------------------------
Reporter: [EMAIL PROTECTED] | Owner: jacob
Status: reopened | Component: Uncategorized
Version: 0.96 | Resolution:
Keywords: | Stage: Unreviewed
Has_patch: 0 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
--------------------------------+-------------------------------------------
Changes (by [EMAIL PROTECTED]):
* status: closed => reopened
* resolution: worksforme =>
Comment:
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.
--
Ticket URL: <http://code.djangoproject.com/ticket/4022#comment:4>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---