Я начал проект в django, и у меня проблемы с импортом. Думаю, проблема вызвана цикличностью импорта. Я менял код разными способами, но решить не могу.
Ошибка:
File "/home/Workspace/vesta/vesta/urls.py", line 8, in <module>
from user.api import UserModelViewSet
File "/home/Workspace/vesta/user/api.py", line 8, in <module>
from user.serializers import UserSerializer
File "/home/Workspace/vesta/user/serializers.py", line 5, in <module>
from core.serializers import (UUIDSerializer)
File "/mhome/Workspace/vesta/core/serializers.py", line 2, in <module>
from user.serializers import UserSerializer
ImportError: cannot import name 'UserSerializer' from 'user.serializers' (/home/Workspace/vesta/user/serializers.py)
И код:
vesta / vesta / urls.py
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import include, re_path
from rest_framework.routers import DefaultRouter
from rest_framework_simplejwt.views import (TokenObtainPairView, TokenRefreshView, TokenVerifyView)
from core.api.schemas.schema_view import schema_view
from user.api import UserModelViewSet
router = DefaultRouter()
router.register(r'user', UserModelViewSet, basename='user')
urlpatterns = [
# API documentation and playground
re_path(r'^api-doc/$', schema_view.with_ui('redoc', cache_timeout=0), name='api-doc'),
re_path(r'^api-playground(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0),
name='api-playground-json'),
re_path(r'^api-playground/$', schema_view.with_ui('swagger', cache_timeout=0), name='api-playground-ui'),
# Admin
re_path(r'admin/', admin.site.urls),
# Authentication token
re_path(r'auth/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
re_path(r'auth/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
re_path(r'auth/token/verify/', TokenVerifyView.as_view(), name='token_verify'),
# API
re_path(r'', include(router.urls))
]
urlpatterns += staticfiles_urlpatterns()
vesta / user / api.py
from django.utils.decorators import method_decorator
from drf_yasg.utils import swagger_auto_schema
from rest_framework import mixins
from core.api.parameters import query_parameter
from core.viewsets import BaseModelViewSet
from user.models import User
from user.serializers import UserSerializer
@method_decorator(name='retrieve', decorator=swagger_auto_schema(manual_parameters=[query_parameter]))
class UserModelViewSet(mixins.RetrieveModelMixin,
BaseModelViewSet):
lookup_field = 'email'
lookup_value_regex = r'[\w.@+-]+'
action_serializers = {
'retrieve': UserSerializer
}
def get_queryset(self):
return User.objects.filter(id=self.request.user.id)
vesta / user / serializers.py
from django_restql.mixins import DynamicFieldsMixin
from rest_framework import serializers
from core.serializers import ModificationSerializer
from core.serializers import (UUIDSerializer)
from group.serializers import GroupSerializer
from user.models import User
class UserSerializer(DynamicFieldsMixin, serializers.HyperlinkedModelSerializer, UUIDSerializer,
ModificationSerializer):
groups = GroupSerializer(many=True)
class Meta(UUIDSerializer.Meta, ModificationSerializer.Meta):
model = User
fields = UUIDSerializer.Meta.fields + ModificationSerializer.Meta.fields + ['email', 'first_name', 'last_name',
'groups']
vesta / core / serializers.py
from rest_framework.serializers import BaseSerializer
from user.serializers import UserSerializer
class UUIDSerializer(BaseSerializer):
class Meta:
fields = ['uuid']
class ModificationSerializer(BaseSerializer):
modified_by = UserSerializer()
class Meta:
fields = ['modified_at', 'modified_by']
Дерево
vesta
├── core
│ ├── api
│ │ ├── __init__.py
│ │ ├── parameters
│ │ │ ├── __init__.py
│ │ │ └── query_parameter.py
│ │ └── schemas
│ │ ├── __init__.py
│ │ └── schema_view.py
│ ├── apps.py
│ ├── __init__.py
│ ├── models
│ │ ├── creation_model.py
│ │ ├── __init__.py
│ │ ├── modification_model.py
│ │ └── uuid_model.py
│ ├── serializers.py # <---------------------- Here (UseSerializer)
│ └── viewsets
│ ├── base_model_view_set.py
│ └── __init__.py
├── group
│ ├── admin
│ │ ├── group_admin.py
│ │ └── __init__.py
│ ├── apps.py
│ ├── __init__.py
│ ├── models
│ │ ├── group_model.py
│ │ └── __init__.py
│ └── serializers
│ ├── group_serializer.py
│ └── __init__.py
├── manage.py
├── requirements.txt
├── user
│ ├── admin
│ │ ├── __init__.py
│ │ └── user_admin.py
│ ├── api.py # <---------------------- Here (UseSerializer)
│ ├── apps.py
│ ├── __init__.py
│ ├── managers
│ │ ├── __init__.py
│ │ └── user_manager.py
│ ├── models
│ │ ├── __init__.py
│ │ └── user_model.py
│ └── serializers.py
└── vesta
├── asgi.py
├── __init__.py
├── settings
│ ├── base.py
│ ├── __init__.py
│ ├── local.py
│ └── third_party
│ ├── __init__.py
│ ├── rest_framework.py
│ └── swagger.py
├── urls.py
└── wsgi.py
Думаю, что проблема находится между файлом vesta / user / api.py и vesta / core / serializers.py .
Как я могу это решить?
Большое спасибо :)