ajwood
ajwood

Reputation: 19057

Breaking ties in django template dictsort

I've got models like this:

class Subject(models.Model):
   sid = models.CharField(max_length=5)

class TimePoint(models.Model):
   tpid = models.CharField(max_length=3)
   subject = models.ForeignKey(Subject)

I'm using the default ListView with queryset=TimePoint.objects.all(). In my template, I'm iterating over the list with {% for tp in timepoint_list|dictsort:"subject.sid" %}. The order of multiple timepoints referencing the same subject is coming out of dictsort unpredictably.

How can I implement the "tie breaker" for timepoints that reference the same subject

Upvotes: 1

Views: 1171

Answers (1)

dhackner
dhackner

Reputation: 3050

Try sorting by tpid first:

{% for tp in timepoint_list|dictsort:"tpid"|dictsort:"subject.sid" %}

Upvotes: 1

Related Questions