django-tables2中的非查询数据排序 [英] Non-queryset data ordering in django-tables2
问题描述
文档说:
表由模型支持,数据库将处理订单。如果不是这种情况,则使用Python cmp函数,并且在比较不同类型时使用以下机制作为回退:...
但是,在自定义列中,模型支持 的表格中是否可能?例如
class MyModel(models.Model):
x = models.IntegerField()
y = models。 IntegerField()
def z(self):
return x + y
class MyTable(tables.Table):
z = tables.Column )
class Meta:
model = MyModel
当我尝试这样的东西,列显示OK,但是当我点击列标题进行排序时,我会收到以下错误:
在呈现时捕获FieldError:不能将关键字u'z'解析成字段。选择是:...
显然这是因为在数据库表中找不到z。
有没有办法?
如果你是对没有数据库列的属性进行排序。您可以将列表传递给您的表格。
假设您的models.py如下所示:
from django.db import models
class MyModel(models.Model):
def foo(self):
return something_complex()
您可以将tables.py看起来像这样:
导入django_tables2作为表
from .models import MyModel
class MyModelTable(tables.Table):
foo = tables.Column ()
class Meta:
model = MyModel
然后在您的views.py:
from django_tables2.config import RequestConfig
from django.core.paginator import InvalidPage
从django.shortcuts import render
def view_my_models(request):
#使用列表,所以django_tables2在内存中排序
my_models = list(MyModel.objects.all() )
my_models_table = MyModelTable(my_models)
RequestConfig(request).configure(my_models_table)
try:
page_number = int(request.GET.get('page'))
except(ValueError,TypeError):
page_number = 1
try:
my_models_table.paginate(page = page_number,per_page = 10)
除了InvalidPage:
my_models_table.paginate(page = 1 ,per_page = 10)
template_vars = {'table':my_models_table}
return render(response,view_my_models.html,template_vars)
还有 an公开机票讨论此问题。
The docs say:
Where the table is backed by a model, the database will handle the ordering. Where this is not the case, the Python cmp function is used and the following mechanism is used as a fallback when comparing across different types: ...
But is this possible in a table that is backed by a model, on a custom column? e.g.
class MyModel(models.Model):
x = models.IntegerField()
y = models.IntegerField()
def z(self):
return x+y
class MyTable(tables.Table):
z = tables.Column()
class Meta:
model = MyModel
When I try something like this, the column displays OK, but when I click on the column header to sort, I get this error:
Caught FieldError while rendering: Cannot resolve keyword u'z' into field. Choices are: ...
Apparently this is because z is not found in the database table.
Is there a way around this?
You can't use a queryset if you're ordering on an attribute that doesn't have a database column. You can pass a list to your table though.
Assuming your models.py looks like this:
from django.db import models
class MyModel(models.Model):
def foo(self):
return something_complex()
You could have tables.py that looks like this:
import django_tables2 as tables
from .models import MyModel
class MyModelTable(tables.Table):
foo = tables.Column()
class Meta:
model = MyModel
Then in your views.py:
from django_tables2.config import RequestConfig
from django.core.paginator import InvalidPage
from django.shortcuts import render
def view_my_models(request):
# use a list so django_tables2 sorts in memory
my_models = list(MyModel.objects.all())
my_models_table = MyModelTable(my_models)
RequestConfig(request).configure(my_models_table)
try:
page_number = int(request.GET.get('page'))
except (ValueError, TypeError):
page_number = 1
try:
my_models_table.paginate(page=page_number, per_page=10)
except InvalidPage:
my_models_table.paginate(page=1, per_page=10)
template_vars = {'table': my_models_table}
return render(response, "view_my_models.html", template_vars)
There's also an open ticket discussing this issue.
这篇关于django-tables2中的非查询数据排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!