从数据库中获取所有配置文件时,出现以下错误
Traceback (most recent call last):
File "/home//venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/venv/lib/python3.7/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/home/venv/lib/python3.7/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/venv/lib/python3.7/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/venv/lib/python3.7/site-packages/rest_framework/generics.py", line 241, in get
return self.list(request, *args, **kwargs)
File "/home/venv/lib/python3.7/site-packages/rest_framework/mixins.py", line 48, in list
return Response(serializer.data)
File "/home/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 768, in data
ret = super(ListSerializer, self).data
File "/home/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "/home/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 686, in to_representation
self.child.to_representation(item) for item in iterable
TypeError: 'ModelBase' object is not iterable
[11/Jul/2019 10:15:06] "GET /profile/ HTTP/1.1" 500 24593
Internal Server Error: /profile/
我可以使用post方法轻松创建配置文件,但是获取所有配置文件是有问题的。我的 models.py 文件
class Profile(TimeModel):
user = models.OneToOneField('user_accounts.CustomUser', primary_key=True, on_delete=models.CASCADE)
first_name = models.CharField(max_length=30, null=True)
last_name = models.CharField(max_length=30, null=True)
birthdate = models.DateTimeField(null=True)
image = models.ImageField(upload_to='profile_images', null=True)
def __str__(self):
return self.user.email
在 serializers.py
中class ProfileSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only=True, source='user.email')
character = CharacterSerializer()
category = CategorySerializer()
birthdate = serializers.DateTimeField(format='%Y-%m-%d')
class Meta:
model = Profile
fields = ['user', 'first_name', 'last_name', 'birthdate', 'gender', 'image', 'category', 'character']
我查看了一些解决方案,但是他们并没有帮助解决它。以及,我添加了
bases =(models.Model,)进行迁移
它什么也没做。我该如何解决这个问题?