我开始使用Django,并且能够使Django Rest Framework工作。现在,我正在尝试使用https://django-elasticsearch-dsl-drf.readthedocs.io/en/0.18/进行Elasticsearch, 跟随https://github.com/barseghyanartur/django-elasticsearch-dsl-drf/tree/master/examples/simple
我遵循了这些示例,并按照这些原则构建了我的项目。当我尝试启动服务器时,我收到ImportError,请问任何人可以查看一下,并向我提供有关我可能缺少的内容的指导。随函附上一些有关环境的详细信息,以供参考。
谢谢
环境:
cmd > pip freeze | find "Django"
django-cors-headers==3.0.2
django-elasticsearch-dsl==0.5.1
django-elasticsearch-dsl-drf==0.18
django-filter==2.1.0
django-nine==0.2.2
djangorestframework==3.9.4
demosite
demosite
settings.py [INSTALLED_APPS contains my_rest_app, search_indexes]
my_rest_app [which works with http://localhost:8000/my_rest_app/]
search_indexes
viewsets
publisher.py [see below]
urls.py [see below]
documents
publisher.py [see below]
search_indexes/url.py
---------------------
from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter
from search_indexes.viewsets import PublisherDocumentViewSet
urlpatterns = [
url(r'^', include(router.urls)),
]
# **********************************************************
# *********************** Publishers ***********************
# **********************************************************
router.register(
r'publishers',
PublisherDocumentViewSet,
basename='publisherdocument'
)
search_indexes/viewsets/publisher.py
------------------------------------
from django_elasticsearch_dsl_drf.pagination import LimitOffsetPagination
from django_elasticsearch_dsl_drf.viewsets import DocumentViewSet
from ..documents import PublisherDocument
from ..serializers import PublisherDocumentSerializer
__all__ = (
'PublisherDocumentViewSet',
)
class PublisherDocumentViewSet(DocumentViewSet):
"""The PublisherDocument view."""
document = PublisherDocument
serializer_class = PublisherDocumentSerializer
search_indexes/documents/publisher.py
--------------------------------------
from my_rest_app.models import Publisher
__all__ = ('PublisherDocument',)
INDEX = Index(settings.ELASTICSEARCH_INDEX_NAMES[__name__])
# See Elasticsearch Indices API reference for available settings
INDEX.settings(
number_of_shards=1,
number_of_replicas=1,
blocks={'read_only_allow_delete': False},
# read_only_allow_delete=False
)
@INDEX.doc_type
class PublisherDocument(DocType):
"""Publisher Elasticsearch document."""
search_indexes/serializers/publisher.py
---------------------------------------
from ..documents import PublisherDocument
__all__ = (
'PublisherDocumentSerializer',
'PublisherDocumentSimpleSerializer',
)
class PublisherDocumentSerializer(serializers.Serializer):
and
class PublisherDocumentSimpleSerializer(DocumentSerializer):
"""Serializer for Publisher document."""
---------demosite/urls.py
---------demosite/urls.py
from django.contrib import admin
from django.urls import include, path
from django.views.generic import RedirectView
#from search_indexes import urls as search_index_urls
urlpatterns = [
path('my_rest_app/', include('my_rest_app.urls')),
path('search_indexes/', include('search_indexes.urls')),
path('admin/', admin.site.urls),
path('', RedirectView.as_view(url='/my_rest_app/', permanent=True)),
#path('', RedirectView.as_view(url='/search_indexes/', permanent=True)),
]
--------- stack trace -------
File "C:\apps\Python37-32\lib\site-packages\django\urls\resolvers.py", line 572, in urlconf_module
return import_module(self.urlconf_name)
File "C:\apps\Python37-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\demosite\demosite\urls.py", line 25, in <module>
path('search_indexes/', include('search_indexes.urls')),
File "C:\apps\Python37-32\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\apps\Python37-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\demosite\search_indexes\urls.py", line 3, in <module>
from search_indexes.viewsets import PublisherDocumentViewSet
ImportError: cannot import name 'PublisherDocumentViewSet' from 'search_indexes.viewsets' (unknown location)
答案 0 :(得分:0)
它显示了导入PublisherDocumentViewSet cannot import name 'PublisherDocumentViewSet' from 'search_indexes.viewsets'
时发生的错误。
尝试将search_indexes / url.py中的导入更改为:
from search_indexes.viewsets.publisher.py import PublisherDocumentViewSet
或
from .viewsets import PublisherDocumentViewSet
如果在文件夹视图集内的文件Publisher.py中定义了类PublisherDocumentViewSet,则应解决此错误。
编辑:就像评论中提到的@Iain Shelvington一样,所有文件夹都应在其中包含__init__.py
文件。如果您检查存储库中的文件夹,则它具有一个init文件。 Github!