我正在尝试使用django-rest-framework-mongoengine来构建使用Django和mongodb的Web服务。
当我添加一个列出Document类型对象的网址时出错。
我的模型文件是
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.
答案 0 :(得分: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'
答案 1 :(得分:2)
尝试在路由器寄存器中添加base_name。
router = routers.MongoSimpleRouter()
router.register(r'jobs', views.JobViewSet, base_name='job-view')