1
0
mirror of https://github.com/django/django.git synced 2025-10-24 22:26:08 +00:00

Fixed #8648 -- Admin no longer ignores to_field. Thanks for the help Karen Tracey and SmileyChris.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8823 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Brian Rosner
2008-09-01 22:43:38 +00:00
parent dcb0e8f959
commit ce47d4ab83
7 changed files with 108 additions and 27 deletions

View File

@@ -550,14 +550,21 @@ class ModelChoiceIterator(object):
if self.field.cache_choices:
if self.field.choice_cache is None:
self.field.choice_cache = [
(obj.pk, self.field.label_from_instance(obj))
for obj in self.queryset.all()
self.choice(obj) for obj in self.queryset.all()
]
for choice in self.field.choice_cache:
yield choice
else:
for obj in self.queryset.all():
yield (obj.pk, self.field.label_from_instance(obj))
yield self.choice(obj)
def choice(self, obj):
if self.field.to_field_name:
key = getattr(obj, self.field.to_field_name)
else:
key = obj.pk
return (key, self.field.label_from_instance(obj))
class ModelChoiceField(ChoiceField):
"""A ChoiceField whose choices are a model QuerySet."""
@@ -570,7 +577,7 @@ class ModelChoiceField(ChoiceField):
def __init__(self, queryset, empty_label=u"---------", cache_choices=False,
required=True, widget=None, label=None, initial=None,
help_text=None, *args, **kwargs):
help_text=None, to_field_name=None, *args, **kwargs):
self.empty_label = empty_label
self.cache_choices = cache_choices
@@ -580,6 +587,7 @@ class ModelChoiceField(ChoiceField):
*args, **kwargs)
self.queryset = queryset
self.choice_cache = None
self.to_field_name = to_field_name
def _get_queryset(self):
return self._queryset
@@ -622,7 +630,8 @@ class ModelChoiceField(ChoiceField):
if value in EMPTY_VALUES:
return None
try:
value = self.queryset.get(pk=value)
key = self.to_field_name or 'pk'
value = self.queryset.get(**{key: value})
except self.queryset.model.DoesNotExist:
raise ValidationError(self.error_messages['invalid_choice'])
return value