Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 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: UI/UX:

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 4 years ago.

Download all attachments as: .zip

Change History (6)

Changed 4 years ago by 3point2

comment:1 Changed 4 years ago by 3point2

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

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 Changed 4 years ago by russellm

  • milestone set to 1.3
  • Triage Stage changed from Unreviewed to Accepted

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 Changed 4 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(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 Changed 4 years ago by russellm

(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 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.