我有一个如下定义的Django模型:
class DummyModel(models.Model):
name = models.CharField(max_length=100, name='Name')
description = models.CharField(name='Description', max_length=150)
time_points = models.PositiveIntegerField(name="Time Points")
more_text = models.CharField(max_length=100, name="More Text")
class Meta:
db_table = "dummy"
我想稍后做一些自定义,所以我也有一个表类:
class DummyTable(tables.Table):
class Meta:
model = DummyModel
attrs = {'class': 'paleblue'}
我有一个相应的观点如下:
@login_required(login_url="login/")
def review(request):
table = DummyTable(DummyModel.objects.all())
RequestConfig(request).configure(table)
return render(request, 'review.html', {'reviews': table})
最后,我使用:
将其渲染到我的模板中{% block content %}
{% load static %}
{% load render_table from django_tables2 %}
<div class="function-container">
{% render_table reviews %}
</div>
{% endblock %}
现在这个渲染很好但是当我尝试对time Points
列进行排序时(通过点击列标题),它会出现错误:
Invalid order_by arguments: [u'Time Points']
Request Method: GET
Request URL: http://127.0.0.1:8000/review/?sort=Time+Points
Django Version: 1.10.5
Exception Type: FieldError
Exception Value:
Invalid order_by arguments: [u'Time Points']
但是,如果我将浏览器指向http://127.0.0.1:8000/review/?sort=time_points
,则可行。因此,不知道如何传递正确的字段名称。我尝试更改name
并将verbose_namne
字段添加为:
time_points = models.PositiveIntegerField(name="time_points", verbose_name="Time Points")
但是,这会在no such column: dummy.time_points
答案 0 :(得分:0)
我想出了一种方法,即覆盖我的table
类中的列。所以,现在我的模型看起来像:
class DummyModel(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=150)
time_points = models.PositiveIntegerField()
more_text = models.CharField(max_length=100)
class Meta:
db_table = "dummy"
在我的自定义table
课程中,我有:
class DummyTable(tables.Table):
#Override here to change the column header text
time_points = tables.Column(verbose_name='Time Points')
more_text = tables.Column(verbose_name='More Text')
class Meta:
model = DummyModel
attrs = {'class': 'paleblue'}
现在排序按预期工作。