I have the following model:
class JobView(models.Model):
job = models.ForeignKey(Job)
user = models.ForeignKey('auth.User')
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['-created']
And in a view i’m grabbing the job info:
model = Job.objects.get(pk=id)
In a template I’m doing the following:
{% regroup model.jobview_set.all by user as user_list %}
{% for u in user_list %}
{{ u.grouper.email }}
{% endfor %}
The problem I’m having is that is not really grouping by the user, or more to the point, randomly grouping them by the user.. Sometimes it display the user twice, sometimes 3 times..Is there something I’m missing?
Cheers!
This is because
{% regroup %}does not order its inputs. You’re trying to group by user, but model.jobview_set.all is not ordered by user, so you get random groups depending on how user is ordered.The easiest solution is sort
model.jobview_set.allbefore passing it to the template.Taken directly from django doc’s:
https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#regroup