Skip to content

Commit

Permalink
fix merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
steverecio committed Oct 31, 2023
1 parent 7b34cb2 commit 39c4620
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions taggit/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ def __init__(self, through, model, instance, prefetch_cache_name, ordering=None)
self.prefetch_cache_name = prefetch_cache_name
if ordering is not None:
self.ordering = ordering
else:
elif instance:
# default to ordering by the pk of the through table entry
related_name = self.through.tag.field.related_query_name()
self.ordering = [f"{related_name}__pk"]
else:
self.ordering = []

def is_cached(self, instance):
return self.prefetch_cache_name in instance._prefetched_objects_cache
Expand All @@ -86,20 +88,7 @@ def get_queryset(self, extra_filters=None):
*self.ordering
)

# distinct query doesn't work so we need to manually dedupe the query
kwargs = extra_filters if extra_filters else {}
pks = qs.values_list("pk", flat=True)
preserved = Case(
*[When(pk=pk, then=Value(i)) for i, pk in enumerate(pks)],
output_field=IntegerField(),
)
results = (
self.through.tag.field.related_model.objects.filter(pk__in=pks, **kwargs)
.annotate(ordering=preserved)
.order_by("ordering")
.distinct()
)
return results
return qs

def get_prefetch_queryset(self, instances, queryset=None):
if queryset is not None:
Expand Down Expand Up @@ -382,10 +371,8 @@ def clear(self):
)

def most_common(self, min_count=None, extra_filters=None):
kwargs = extra_filters if extra_filters else {}
queryset = (
self.through.tag_model()
.objects.filter(**kwargs)
self.get_queryset(extra_filters)
.annotate(num_times=models.Count(self.through.tag_relname()))
.order_by("-num_times")
)
Expand Down

0 comments on commit 39c4620

Please sign in to comment.