From bcfc4e56a197340ce3a51dc4cb190a213dc3a860 Mon Sep 17 00:00:00 2001 From: Preeti Yuankrathok Date: Fri, 15 Oct 2021 16:17:38 +0700 Subject: [PATCH] Support for none label field --- dropdown/helpers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dropdown/helpers.py b/dropdown/helpers.py index 1ab0f37..d46b668 100644 --- a/dropdown/helpers.py +++ b/dropdown/helpers.py @@ -14,7 +14,7 @@ def from_model( model, - label_field: str, + label_field: str = None, value_field='pk', q_filter: models.Q = None, no_limit=True, @@ -24,7 +24,7 @@ def from_model( Get dropdown items from given model @param model: model to get dropdown - @param label_field: name of field which will be label + @param label_field: name of field which will be label (default is `__str__`) @param value_field: name of field which will be value (default is `pk`) @param q_filter: additional filter @param no_limit: no items limit (overriding `LIMIT` in settings) @@ -48,7 +48,7 @@ def from_model( queryset = queryset.values(*values) # order - queryset = queryset.order_by(label_field) + queryset = queryset.order_by(label_field or value_field) # distinct queryset = queryset.distinct() @@ -64,7 +64,7 @@ def from_model( # results return [ types.DropdownItem( - label=x[label_field], + label=x[label_field] if label_field is not None else str(x), value=x[value_field], context={y: x[y] for y in (context_fields or [])}, ) for x in result_list