Opened 9 years ago

Closed 9 years ago

#4022 closed (invalid)

Problem with select fields and foreign keys

Reported by: hadsie@… 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: UI/UX:


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


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 Changed 9 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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.

comment:2 Changed 9 years ago by hadsie@…

Ok, thanks. I'll update my app to use the newforms library.

comment:3 Changed 9 years ago by Gary Wilson <gary.wilson@…>

  • Resolution set to worksforme
  • Status changed from new to 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 Changed 9 years ago by hadsie@…

  • Resolution worksforme deleted
  • Status changed from closed to 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 :).

from django.db import models

class Foo(models.Model):
    name = models.CharField(maxlength = 100)

    def __str__(self):

    class Admin:

class Bar(models.Model):
    field1 = models.ForeignKey(Foo)
    field2 = models.CharField(maxlength = 1, choices = (('1', 'first choice'), ('2', 'second choice')))

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:
            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" />


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 Changed 9 years ago by Gary Wilson <gary.wilson@…>

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.

comment:6 Changed 9 years ago by hadsie@…

Hah! Yes, that fixes it. Thanks :)

comment:7 Changed 9 years ago by Gary Wilson <gary.wilson@…>

  • Resolution set to invalid
  • Status changed from reopened to closed

No bug here.

Note: See TracTickets for help on using tickets.
Back to Top