python - How to order a reverse foreign key in django -


i have following for loop create in template:

    {% task in item.task_set.all %}         <li class="task">             {{ task }}         </li>     {% endfor %} 

is there way order task_set. orders id order name of task.

if happy tasks ordered name of task, use ordering option

class task(models.model):     name = models.charfield(max_length=50)      class meta:         ordering = ['name'] 

if want different ordering model's default ordering, set order of tasks in view, iterate through tasks in template.

# view tasks = item.task_set.order_by('name')  # template {% task in tasks %} 

it's not possible change ordering in template, because can't pass arguments {{ item.task_set.order_by }}. if need specify order of queryset in template, you'll need write custom template filter.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -