简体   繁体   English

Mongoengine + Django-tables2:所需的表或查询集,而不是QuerySet

[英]Mongoengine + Django-tables2: Expected table or queryset, not QuerySet

I'm starting to learn django and using it to build and interface to our mongoDB using also mongoengine. 我开始学习django,并使用mongoengine将其用于构建和连接到我们的mongoDB。 I'm following this tutorial to use django-tables2 but I can't even start it because I get the error Expected table or queryset, not QuerySet . 我正在按照本教程使用django-tables2,但是我什至无法启动它,因为我收到了错误的Expected table or queryset, not QuerySet This is the class I'm using: 这是我正在使用的课程:

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

On the view I'm just doing 就我而言,我只是在做

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

I see that mongoengine output is a QuerySet type. 我看到mongoengine的输出是QuerySet类型。 How can I convert it to some type I can input on django-tables? 如何将其转换为可以在Django表上输入的某种类型? Thank you for the help! 感谢您的帮助!

The data format django-tables2 expects is a QuerySet, a list of dicts, or something behaving like that. django-tables2期望的数据格式是QuerySet,字典列表或类似行为。 You can create a class inheriting from TableData , pass that to a vanilla django_tables2.Table and put that in your context instead of Companies.objects.all() . 您可以创建一个继承自TableData的类,将其传递给普通的django_tables2.Table并将其放在您的上下文中,而不是放在Companies.objects.all()

It would look somewhat like this: 它看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM