开发者

Django: django_tables sort on method

开发者 https://www.devze.com 2023-04-05 01:06 出处:网络
I\'m using django-tables (http://pypi.python.org/pypi/django-tables/0.2) to render the contents of a MySQL table.

I'm using django-tables (http://pypi.python.org/pypi/django-tables/0.2) to render the contents of a MySQL table.

The model the table is mapped to features a couple of functions that are conversion functions, or aggregates of the rows contents.

For example:

class Example(models.Model):

    STATUS_1 = 0
    STATUS_2 = 1
    STATUS_3 = 2

    STATUS_CHOICES = ( 
              (STATUS_1, _('Status One')),
              (STATUS_2, _('Status Two')),
              (STATUS_3, _('Status Three')),
              )


    #This gives a nice drop down when rendered in a form...
    status = models.IntegerField(choices=STATUS_CHOICES,
                             default=STATUS_1,
                             verbose_name=_('Status'))

    #This is the function to produce the text for a choice...
    def status_text(self):
        return self.STATUS_CHOICES[self.status][1]

    #A function that inspects items that link to 'Example'
    #and creates an aggregate string value
    def openissues(self):
        answer = _("No")
        linked_issues = self.issues.all()
        for thisissue in linked_issues:
            if (not thisissues.resolved):
                answer = _("Yes")
                break

        return answer

Then in my table class definition I have:

import django_tables
from django.utils.translation import ugettext_lazy as _

class ExampleTable(django_tables.ModelTable):
    .
    .
    status_text = django_tables.Column(verbose_name=_('Status'))
    openissues = django_tables.Column(verbose_name=_('Open Issues'))
    .
    .

My view is built like this:

def decision_list(request):
    .
    .
    objects = Example.objects.all()
    table = ExampleTable(objects, order_by=request.GET.get('sort'))
    return render_to_response('example_list.html',
        RequestContext(request, dict(example=example)))

And finally the template looks like this:

<table id="example-list" cellspacing="0">
<tr>
    {% for column in example.columns %}
<th id="{{ example.name }}"{% if example.is_ordered_straight %} class="sorted straight"{% endif %}{% if column.is_ordered_reverse %} class="sorted reverse"{% endif %}><a href="?sort={{ column.name_toggled }}">{{ column }}</a></th>
    {% endfor %}
</tr>
{% for thisexample in example.rows %}
    <tr>
        <td>{{ thisexample.id }}</td>
        <td><a href="{% url a_url %}">{{thisexample.name}}</a></td>
        <td>{{ thisexample.status_text }}</td>
        <td>{{ thisexample.openissues }}</td>

    </tr>
{% endfor %}
</table>

(Note: I've edited the code a bit to only include the relevant parts, and changed names to more generic things s开发者_如何学运维o it might be easier to understand)

Anyway, as you can hopefully see I want to be able to sort on the methods 'status_text()' and 'openissues()' when the user clicks the column headers.

This doesn't work.

Documentation for django_tables says:

"Custom columns not based on a model field do not support ordering, regardless of the sortable property (it is ignored)."

Is there a way I can let the user sort on the model functions? This seems like something that a lot of people would want to do.


An important property of django's ORM layer is that sorting and filtering of QuerySet objects is always performed on the DBMS. A method or property in a model class does is evaluated on the application server and does not map to a database column. Hence you can't sort on these.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号