157π
β
You need to specify the ordering in the attendee model, like this. For example (assuming your model class is named Attendee):
class Attendee(models.Model):
class Meta:
ordering = ['last_name']
See the manual for further reference.
EDIT. Another solution is to add a property to your Event model, that you can access from your template:
class Event(models.Model):
# ...
@property
def sorted_attendee_set(self):
return self.attendee_set.order_by('last_name')
You could define more of these as you need themβ¦
π€tawmas
174π
You can use template filter dictsort https://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatefilter-dictsort
This should work:
{% for event in eventsCollection %}
{{ event.location }}
{% for attendee in event.attendee_set.all|dictsort:"last_name" %}
{{ attendee.first_name }} {{ attendee.last_name }}
{% endfor %}
{% endfor %}
π€tariwan
- [Django]-How to produce a 303 Http Response in Django?
- [Django]-How can I create a deep clone of a DB object in Django?
- [Django]-FileUploadParser doesn't get the file name
8π
One solution is to make a custom templatag:
@register.filter
def order_by(queryset, args):
args = [x.strip() for x in args.split(',')]
return queryset.order_by(*args)
use like this:
{% for image in instance.folder.files|order_by:"original_filename" %}
...
{% endfor %}
π€matinfo
- [Django]-Django unit tests without a db
- [Django]-Creating a JSON response using Django and Python
- [Django]-How can I filter a Django query with a list of values?
0π
regroup should be able to do what you want, but is there a reason you canβt order them the way you want back in the view?
π€Tom
- [Django]-Django-Bower + Foundation 5 + SASS, How to configure?
- [Django]-Get object by field other than primary key
- [Django]-How to dynamically compose an OR query filter in Django?
Source:stackexchange.com