Django:django_tables 排序方法

发布于 2024-12-04 16:46:41 字数 2874 浏览 0 评论 0原文

我正在使用 django-tables (http://pypi.python.org/pypi/django-tables/0.2) 来呈现 MySQL 表的内容。

表映射到的模型具有几个函数,这些函数是转换函数或行内容的聚合。

例如:

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

然后在我的表类定义中,我有:

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'))
    .
    .

我的视图是这样构建的:

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)))

最后模板看起来像这样:(

<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>

注意:我对代码进行了一些编辑,仅包含相关部分,并将名称更改为 more通用的东西,所以可能更容易理解)

无论如何,正如您希望看到的那样,当用户单击列标题时,我希望能够对“status_text()”和“openissues()”方法进行排序。

这是行不通的。

django_tables 的文档说:

“不基于模型字段的自定义列不支持排序,无论 sortable 属性如何(都会被忽略)。”

有没有办法让用户对模型函数进行排序?这似乎是很多人都想做的事情。

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 so 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁与争疯 2024-12-11 16:46:41

django ORM 层的一个重要属性是 QuerySet 对象的排序和过滤始终在 DBMS 上执行。
模型类中的方法或属性会在应用程序服务器上求值,但不会映射到数据库列。因此你无法对这些进行排序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文