Skip to content

Commit

Permalink
Merge branch 'select-related' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
earthpyy committed Oct 18, 2021
2 parents 48f73f6 + 3fc3ae9 commit 464081f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dropdown/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def from_model(
q_filter: models.Q = None,
no_limit=True,
context_fields: typing.List[str] = None,
select_related: typing.List[str] = None,
) -> typing.Tuple[typing.List[types.DropdownItem], int]:
"""
Get dropdown items from given model
Expand All @@ -30,6 +31,7 @@ def from_model(
@param q_filter: additional filter
@param no_limit: no items limit (overriding `LIMIT` in settings)
@param context_fields: additional fields to be appear in context in each dropdown item
@param select_related: fields to select related
@return: tuple of dropdown items and item count
"""
if context_fields is None:
Expand All @@ -38,6 +40,11 @@ def from_model(
# initial queryset
queryset = model.objects.all()

# select related
# NOTE: prefetch related is not supported
if select_related is not None:
queryset = queryset.select_related(*select_related)

# filter
if q_filter:
queryset = queryset.filter(q_filter)
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions tests/utils/test_dot_to_relation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from dropdown import utils


def test_simple():
assert utils.dot_to_relation('x.y') == 'x__y'


def test_three_dots():
assert utils.dot_to_relation('x.y.z') == 'x__y__z'

0 comments on commit 464081f

Please sign in to comment.