我无法弄清楚这一点。
我试图从'帐户'导入我的UserSerializer()应用程序,以便我可以在我的CommentSerializer()中使用它来评论'应用
如果我尝试嵌套另一个序列化程序,例如LikeSerializer()来自'喜欢'应用程序,没有问题。
如果你注意到comments.serializers.py,我可以取消注释LikeSerializer部分,并注释UserSerializer,没有错误。
但是一旦我取消注释UserSerializer(),我就会点击ImportError: cannot import name UserSerializer
。
我的文件夹结构:
/apps
/accounts
serializers.py
/comments
serializers.py
** accounts.serializers.py
from django.contrib.auth import update_session_auth_hash
from rest_framework import serializers
from .models import User, ProfilePic
from ..memories.serializers import MemorySerializer
class UserSerializer(serializers.ModelSerializer):
memories = MemorySerializer(many=True,read_only=True)
password = serializers.CharField(write_only=True, required=False)
# password2 = serializers.CharField(write_only=True, required=False)
class Meta:
model = User
fields = ('id', 'email', 'first_name', 'last_name', 'created_at', 'relation', 'password', 'memories',)
read_only_fields = ('created_at',)
def create(self, validated_data):
return User.objects.create_user(**validated_data)
def update(self, instance, validated_data):
instance.email = validated_data.get('email', instance.email)
instance.first_name = validated_data.get('first_name', instance.first_name)
isntance.last_name = validated_data.get('last_name', instance.last_name)
instance.relation = validated_data.get('relation', instance.relation)
instance.save()
password = validated_data.get('password', instance.password)
# password2 = validated_data.get('password2', instance.password2)
# if password and password2 and password == password2:
# instance.set_password(password)
# instance.save()
update_session_auth_hash(self.context.get('request'), instance)
return instance
** comments.serializers.py
from .models import Comment
from ..accounts.serializers import UserSerializer
# from ..likes.serializers import LikeSerializer
from rest_framework import serializers
class CommentSerializer(serializers.ModelSerializer):
author = UserSerializer()
# likes = LikeSerializer(many=True,read_only=True)
# sub_comments = serializers.CommentSerializer()
class Meta:
model = Comment
fields = ('id','author','memory','content','sub_comments', 'likes','created_at')
read_only = ('memory','created_at',)
depth = 1
**回溯
Unhandled exception in thread started by <function wrapper at 0x1037e1cf8>
Traceback (most recent call last):
File "/Users/bchuey/.environments/dm_lin/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/bchuey/.environments/dm_lin/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/Users/bchuey/.environments/dm_lin/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/Users/bchuey/.environments/dm_lin/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/bchuey/.environments/dm_lin/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/bchuey/.environments/dm_lin/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/Users/bchuey/.environments/dm_lin/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/bchuey/.environments/dm_lin/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/bchuey/.environments/dm_lin/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/bchuey/.environments/dm_lin/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/bchuey/Documents/digital_memorials/dm_you_lin/src/digital_memorial/urls.py", line 19, in <module>
from apps.accounts.views import UserViewSet, ProfilePicViewSet, LoginView
File "/Users/bchuey/Documents/digital_memorials/dm_you_lin/src/apps/accounts/views.py", line 5, in <module>
from .serializers import UserSerializer, ProfilePicSerializer
File "/Users/bchuey/Documents/digital_memorials/dm_you_lin/src/apps/accounts/serializers.py", line 5, in <module>
from ..memories.serializers import MemorySerializer
File "/Users/bchuey/Documents/digital_memorials/dm_you_lin/src/apps/memories/serializers.py", line 3, in <module>
from ..comments.serializers import CommentSerializer
File "/Users/bchuey/Documents/digital_memorials/dm_you_lin/src/apps/comments/serializers.py", line 4, in <module>
from ..accounts.serializers import UserSerializer
ImportError: cannot import name UserSerializer
答案 0 :(得分:0)
附加&#39; apps&#39;文件夹到您的python路径并尝试从accounts.serializers导入?您还可以从其他导入中删除所有前导..(前提是它们是应用程序的子目录)
** accounts.serializers.py
import sys
sys.path.append("/Users/bchuey/Documents/digital_memorials/dm_you_lin/src/apps")
from django.contrib.auth import update_session_auth_hash
from rest_framework import serializers
from .models import User, ProfilePic
from memories.serializers import MemorySerializer
class UserSerializer(serializers.ModelSerializer):
memories = MemorySerializer(many=True,read_only=True)
password = serializers.CharField(write_only=True, required=False)
password2 = serializers.CharField(write_only=True, required=False)
class Meta:
model = User
fields = ('id', 'email', 'first_name', 'last_name', 'created_at', 'relation', 'password', 'memories',)
read_only_fields = ('created_at',)
def create(self, validated_data):
return User.objects.create_user(**validated_data)
def update(self, instance, validated_data):
instance.email = validated_data.get('email', instance.email)
instance.first_name = validated_data.get('first_name', instance.first_name)
instance.last_name = validated_data.get('last_name', instance.last_name)
instance.relation = validated_data.get('relation', instance.relation)
instance.save()
password = validated_data.get('password', instance.password)
password2 = validated_data.get('password2', instance.password2)
if password and password2 and password == password2:
instance.set_password(password)
instance.save()
update_session_auth_hash(self.context.get('request'), instance)
return instance
** comments.serializers.py
import sys
sys.path.append("/Users/bchuey/Documents/digital_memorials/dm_you_lin/src/apps")
from .models import Comment
from accounts.serializers import UserSerializer
from likes.serializers import LikeSerializer
from rest_framework import serializers
class CommentSerializer(serializers.ModelSerializer):
author = UserSerializer()
likes = LikeSerializer(many=True,read_only=True)
sub_comments = serializers.CommentSerializer()
class Meta:
model = Comment
fields = ('id','author','memory','content','sub_comments', 'likes','created_at')
read_only = ('memory','created_at',)
depth = 1