Django

Code

Changeset 1575

Show
Ignore:
Timestamp:
12/08/05 20:25:17 (3 years ago)
Author:
adrian
Message:

Fixed #1017 -- Admin now selects single choice for ForeignKey? fields if only one choice is available. Thanks for reporting, Eugene

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/meta/fields.py

    r1573 r1575  
    728728        if not obj: 
    729729            # In required many-to-one fields with only one available choice, 
    730             # select that one available choice. Note: We have to check that 
    731             # the length of choices is *2*, not 1, because SelectFields always 
    732             # have an initial "blank" value. 
    733             if not self.blank and not self.rel.raw_id_admin and self.choices: 
     730            # select that one available choice. Note: For SelectFields 
     731            # (radio_admin=False), we have to check that the length of choices 
     732            # is *2*, not 1, because SelectFields always have an initial 
     733            # "blank" value. Otherwise (radio_admin=True), we check that the 
     734            # length is 1. 
     735            if not self.blank and (not self.rel.raw_id_admin or self.choices): 
    734736                choice_list = self.get_choices_default() 
    735                 if len(choice_list) == 2: 
     737                if self.radio_admin and len(choice_list) == 1: 
     738                    return {self.attname: choice_list[0][0]} 
     739                if not self.radio_admin and len(choice_list) == 2: 
    736740                    return {self.attname: choice_list[1][0]} 
    737741        return Field.flatten_data(self, follow, obj)