Skip to content

Commit

Permalink
Support for none label field
Browse files Browse the repository at this point in the history
  • Loading branch information
earthpyy committed Oct 15, 2021
1 parent 1346c6f commit bcfc4e5
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions dropdown/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit bcfc4e5

Please sign in to comment.