Mongoengine+Djangotables2:需要表或queryset,而不是Query

2024-05-14 16:47:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我开始学习django,并使用它来构建mongoDB并使用mongoengine与之连接。 我跟随this tutorial使用django-tables2,但是我甚至不能启动它,因为我得到了错误Expected table or queryset, not QuerySet。 这是我正在使用的类:

class Companies(Document):
    url = StringField(required=True, unique=True)
    name = StringField(required=True)
    founded = IntField()
    headquarters = EmbeddedDocumentField(HQ)
    description = StringField()

在我正在做的风景上

def companies(request):
    return render(request, 'toolbox/companies.html', {'companies': Companies.objects.all()})

我看到mongoengine输出是QuerySet类型。如何将其转换为可以在django表上输入的类型? 谢谢你的帮助!你知道吗


Tags: djangotrue类型requestmongodb错误requiredthis
1条回答
网友
1楼 · 发布于 2024-05-14 16:47:28

django-tables2期望的数据格式是QuerySet、dict列表或类似的行为。您可以创建一个继承自^{}的类,将其传递给普通的django_tables2.Table,并将其放在上下文中,而不是Companies.objects.all()。你知道吗

它看起来有点像这样:

import django_tables2 as tables
from django_tables2.data import TableQuerysetData


class TableDocumentData(TableQuerysetData):
    # not sure what to override here, since I do not know the mongoengine API at all


def companies(request):
    table = CompanyTable(data=TableDocumentData(Companies.objects.all()))

    return render(request, 'toolbox/companies.html', {'companies': table})

相关问题 更多 >

    热门问题