Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango1.4: How to use order_by in template?
    primarykey
    data
    text
    <p>Django1.4: How to use order_by in template?</p> <p>models.py</p> <pre><code>from django.db import models from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic class Note(models.Model): contents = models.TextField() writer = models.ForeignKey(User, to_field='username') date = models.DateTimeField(auto_now_add=True) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') class Customer(models.Model): name = models.CharField(max_length=50,) notes = generic.GenericRelation(Note, null=True) </code></pre> <p>Above is my models.py.</p> <p>I want to use 'order_by'(<a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by" rel="nofollow">https://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by)</a></p> <p>And...</p> <p>views.py</p> <pre><code>from django.views.generic import DetailView from crm.models import * class customerDetailView(DetailView): context_object_name = 'customerDetail' template_name = "customerDetail.html" allow_empty = True model = Customer slug_field = 'name' </code></pre> <p>My views.py use DetailView(<a href="https://docs.djangoproject.com/en/1.4/ref/class-based-views/#detailview" rel="nofollow">https://docs.djangoproject.com/en/1.4/ref/class-based-views/#detailview</a>).</p> <p>And</p> <p>customerDetail.html</p> <pre><code>&lt;table class="table table-bordered" style="width: 100%;"&gt; &lt;tr&gt; &lt;td&gt;Note&lt;/td&gt; &lt;/tr&gt; {% for i in customerDetail.notes.all.order_by %}&lt;!-- It's not working --&gt; &lt;tr&gt; &lt;th&gt;({{ i.date }}) {{ i.contents }}[{{ i.writer }}]&lt;/th&gt; &lt;/tr&gt; {% endfor %} &lt;/table&gt; </code></pre> <p>I want to use order_by in template...</p> <p>What should I do?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload