ModelForms (and hence the admin) no longer honor the default value for a ForeignKey
|Reported by:||3point2||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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.
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().
Change History (6)
Changed 5 years ago by 3point2
comment:1 Changed 5 years ago by 3point2
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:2 Changed 5 years ago by russellm
- milestone set to 1.3
- Triage Stage changed from Unreviewed to Accepted