From 58220b17d64e761b2d7195417688f90a5724fb44 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Fri, 9 Dec 2005 02:25:17 +0000 Subject: [PATCH] Fixed #1017 -- Admin now selects single choice for ForeignKey fields if only one choice is available. Thanks for reporting, Eugene git-svn-id: http://code.djangoproject.com/svn/django/trunk@1575 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/meta/fields.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/django/core/meta/fields.py b/django/core/meta/fields.py index 2342c137c6..c761295f33 100644 --- a/django/core/meta/fields.py +++ b/django/core/meta/fields.py @@ -727,12 +727,16 @@ class ForeignKey(Field): def flatten_data(self, follow, obj=None): if not obj: # In required many-to-one fields with only one available choice, - # select that one available choice. Note: We have to check that - # the length of choices is *2*, not 1, because SelectFields always - # have an initial "blank" value. - if not self.blank and not self.rel.raw_id_admin and self.choices: + # select that one available choice. Note: For SelectFields + # (radio_admin=False), we have to check that the length of choices + # is *2*, not 1, because SelectFields always have an initial + # "blank" value. Otherwise (radio_admin=True), we check that the + # length is 1. + if not self.blank and (not self.rel.raw_id_admin or self.choices): choice_list = self.get_choices_default() - if len(choice_list) == 2: + if self.radio_admin and len(choice_list) == 1: + return {self.attname: choice_list[0][0]} + if not self.radio_admin and len(choice_list) == 2: return {self.attname: choice_list[1][0]} return Field.flatten_data(self, follow, obj)