Opened 15 years ago

Closed 14 years ago

Last modified 13 years ago

#13679 closed (fixed)

ModelForms (and hence the admin) no longer honor the default value for a ForeignKey

Reported by: 3point2 Owned by: nobody
Component: Forms Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In Django 1.1, it was possible to use a callable to set the default value on a ForeignKey field. This functionality was broken in r12721 and doesn't currently work in Django 1.2.

Example:

def get_default():
    return SomeModel.objects.get(id=5)

class Test(models.Model):
    foo = models.ForeignKey(SomeModel, default=get_default)

This model should display in the admin with the default SomeModel already selected in the drop-down box. The ModelForm code that does this seems to have been confused by the changes made in r12721.

The code in the Field class in django/db/models/fields/__init__.py could also do with a cleanup: formfield() first checks has_default() before calling get_default(), which also checks has_default(). The patch in r12721 adds to this duplication of logic by checking if self.default is callable, which is also done by get_default().

Attachments (1)

modelform.tar.bz2 (2.7 KB ) - added by 3point2 14 years ago.

Download all attachments as: .zip

Change History (6)

by 3point2, 14 years ago

Attachment: modelform.tar.bz2 added

comment:1 by 3point2, 14 years ago

i've attached a simple test case: it's a tarball of a django project called modelform, which contains an app called testing.

the only files i've modified from the defaults created by "django-admin.py startproject" and "./manage.py startapp" are settings.py (db is set to sqlite), models.py, and tests.py.

running "./manage.py test" passes under django 1.1.1 and fails under 1.1.2 and 1.2.1.

comment:2 by Russell Keith-Magee, 14 years ago

milestone: 1.3
Triage Stage: UnreviewedAccepted

Confirmed that the bug exists, and is a regression from 1.1.1->1.1.2. Which means we're going to have a discussion about whether this is enough to simulate a 1.1.3.

I'm working on a patch; the issue is that the initial value is being rendered as unicode(value), but this results in the unicode representation of the object, not the primary key value when the callable returns a queryset. We need special handling here for ModelChoiceField et al, probably in the form of a specialized subclass of HiddenInput that can do the same input manipulation that ModelChoiceIterator does.

comment:3 by Russell Keith-Magee, 14 years ago

Resolution: fixed
Status: newclosed

(In [13577]) Fixed #13679, #13231, #7287 -- Ensured that models that have ForeignKeys/ManyToManyField can use a a callable default that returns a model instance/queryset. #13679 was a regression in behavior; the other two tickets are pleasant side effects. Thanks to 3point2 for the report.

comment:4 by Russell Keith-Magee, 14 years ago

(In [13578]) [1.2.X] Fixed #13679, #13231, #7287 -- Ensured that models that have ForeignKeys/ManyToManyField can use a a callable default that returns a model instance/queryset. #13679 was a regression in behavior; the other two tickets are pleasant side effects. Thanks to 3point2 for the report.

Backport of r13577 from trunk.

comment:15 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

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