3

我正在尝试使用 django-rest-framework-mongoengine 来构建使用 Django 和 mongodb 的 Web 服务。

当我包含一个列出我的 Document 类型对象的 url 时出现错误。

我的模型文件是

class Job(Document):
    title = StringField(required=True)
    state = StringField(required=True)

我的看法是

class JobViewSet(GenericAPIView):
    queryset = Job.objects.all()
    serializer_class = JobSerializer

我的网址文件是

router = routers.MongoSimpleRouter()
router.register(r'jobs', views.JobViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

我的序列化程序是

class JobSerializer(DocumentSerializer):
    class Meta:
        model = Job
        fields = ('title', 'state')

执行 runserver 时出现以下错误

`base_name` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.model` attribute.

我尝试了不同的视图类型,但都是相同的错误。任何帮助是极大的赞赏....

我在用

python 2.7.9
Django==1.7.7 
django-rest-framework-mongoengine==2.0.2    
mongoengine==0.9.0

这是完整的堆栈

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.7.7
Python Version: 2.7.9
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'romeo',
 'mongoengine.django.mongo_auth',
 'jobs',
 'rest_framework',
 'rest_framework_mongoengine')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  98.                 resolver_match = resolver.resolve(request.path_info)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  343.             for pattern in self.url_patterns:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  372.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  366.             self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)
File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_pluginbase.py" in plugin_import
  452.                                    fromlist, level)
File "/Users/amer/Workspace/Repository/romeo/romeo/urls.py" in <module>
  6. router.register(r'jobs', views.JobViewSet)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/routers.py" in register
  60.             base_name = self.get_default_base_name(viewset)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework_mongoengine/routers.py" in get_default_base_name
  11.         assert model_cls, '`base_name` argument not specified, and could ' \

Exception Type: AssertionError at /
Exception Value: `base_name` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.model` attribute.
4

2 回答 2

4

您需要添加 base_name 参数以使其工作:

router = routers.MongoSimpleRouter()
router.register(r'jobs', views.JobViewSet, 'job-view')

我试试这个(来自 Geo Jacob 的回答),但它不起作用

router.register(r'jobs', views.JobViewSet, base_name='job-view')

base_name='job-view'注意:注意和之间的区别'job-view'

于 2016-09-30T18:41:54.277 回答
2

尝试在路由器寄存器中添加一个 base_name。

router = routers.MongoSimpleRouter()
router.register(r'jobs', views.JobViewSet, base_name='job-view')
于 2015-04-02T06:36:41.370 回答