MCPcopy
hub / github.com/django/django / get_choices

Method get_choices

django/db/models/fields/__init__.py:1096–1125  ·  view source on GitHub ↗

Return choices with a default blank choices included, for use as <select> choices for this field.

(
        self,
        include_blank=True,
        blank_choice=None,
        limit_choices_to=None,
        ordering=(),
    )

Source from the content-addressed store, hash-verified

1094 return db_default
1095
1096 def get_choices(
1097 self,
1098 include_blank=True,
1099 blank_choice=None,
1100 limit_choices_to=None,
1101 ordering=(),
1102 ):
1103 """
1104 Return choices with a default blank choices included, for use
1105 as <select> choices for this field.
1106 """
1107 if blank_choice is None:
1108 blank_choice = [("", get_blank_choice_label())]
1109 if self.choices is not None:
1110 if include_blank:
1111 return BlankChoiceIterator(self.choices, blank_choice)
1112 return self.choices
1113 rel_model = self.remote_field.model
1114 limit_choices_to = limit_choices_to or self.get_limit_choices_to()
1115 choice_func = operator.attrgetter(
1116 self.remote_field.get_related_field().attname
1117 if hasattr(self.remote_field, "get_related_field")
1118 else "pk"
1119 )
1120 qs = rel_model._default_manager.complex_filter(limit_choices_to)
1121 if ordering:
1122 qs = qs.order_by(*ordering)
1123 return (blank_choice if include_blank else []) + [
1124 (choice_func(x), str(x)) for x in qs
1125 ]
1126
1127 def value_to_string(self, obj):
1128 """

Callers 5

formfieldMethod · 0.95
field_choicesMethod · 0.45
field_choicesMethod · 0.45
formfieldMethod · 0.45

Calls 6

get_blank_choice_labelFunction · 0.90
BlankChoiceIteratorClass · 0.90
complex_filterMethod · 0.80
order_byMethod · 0.80
get_limit_choices_toMethod · 0.45
get_related_fieldMethod · 0.45

Tested by

no test coverage detected