summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-12-09 02:25:17 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-12-09 02:25:17 +0000
commit58220b17d64e761b2d7195417688f90a5724fb44 (patch)
treea3d0bd0f718bf95c7a49b8102036fc5631dc526f
parentdc2daff31f672a0129b0ed75e47cd4b1f8656aeb (diff)
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
-rw-r--r--django/core/meta/fields.py14
1 files 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)