Simplified Field.get_choices().
This commit is contained in:
parent
f152678d36
commit
bdb747a5f2
|
@ -3,6 +3,7 @@ import copy
|
||||||
import datetime
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
import itertools
|
import itertools
|
||||||
|
import operator
|
||||||
import uuid
|
import uuid
|
||||||
import warnings
|
import warnings
|
||||||
from base64 import b64decode, b64encode
|
from base64 import b64decode, b64encode
|
||||||
|
@ -790,31 +791,25 @@ class Field(RegisterLookupMixin):
|
||||||
Return choices with a default blank choices included, for use
|
Return choices with a default blank choices included, for use
|
||||||
as <select> choices for this field.
|
as <select> choices for this field.
|
||||||
"""
|
"""
|
||||||
blank_defined = False
|
|
||||||
choices = list(self.choices) if self.choices else []
|
|
||||||
named_groups = choices and isinstance(choices[0][1], (list, tuple))
|
|
||||||
if not named_groups:
|
|
||||||
for choice, __ in choices:
|
|
||||||
if choice in ('', None):
|
|
||||||
blank_defined = True
|
|
||||||
break
|
|
||||||
|
|
||||||
first_choice = (blank_choice if include_blank and
|
|
||||||
not blank_defined else [])
|
|
||||||
if self.choices:
|
if self.choices:
|
||||||
return first_choice + choices
|
choices = list(self.choices)
|
||||||
|
if include_blank:
|
||||||
|
named_groups = isinstance(choices[0][1], (list, tuple))
|
||||||
|
blank_defined = not named_groups and any(choice in ('', None) for choice, __ in choices)
|
||||||
|
if not blank_defined:
|
||||||
|
choices = blank_choice + choices
|
||||||
|
return choices
|
||||||
rel_model = self.remote_field.model
|
rel_model = self.remote_field.model
|
||||||
limit_choices_to = limit_choices_to or self.get_limit_choices_to()
|
limit_choices_to = limit_choices_to or self.get_limit_choices_to()
|
||||||
if hasattr(self.remote_field, 'get_related_field'):
|
choice_func = operator.attrgetter(
|
||||||
lst = [(getattr(x, self.remote_field.get_related_field().attname),
|
self.remote_field.get_related_field().attname
|
||||||
smart_text(x))
|
if hasattr(self.remote_field, 'get_related_field')
|
||||||
for x in rel_model._default_manager.complex_filter(
|
else 'pk'
|
||||||
limit_choices_to)]
|
)
|
||||||
else:
|
return (blank_choice if include_blank else []) + [
|
||||||
lst = [(x.pk, smart_text(x))
|
(choice_func(x), smart_text(x))
|
||||||
for x in rel_model._default_manager.complex_filter(
|
for x in rel_model._default_manager.complex_filter(limit_choices_to)
|
||||||
limit_choices_to)]
|
]
|
||||||
return first_choice + lst
|
|
||||||
|
|
||||||
def value_to_string(self, obj):
|
def value_to_string(self, obj):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue