我一起使用react和django,似乎遇到了一个大问题。我在该站点上找到的大多数解决方案都建议检查模板以进行重复,但是我没有使用django的默认模板引擎。当我使用model_to_dict(driver_object)时出现此错误。驱动程序对象有一张我在模型中设置为null = true的照片。请帮我,我不知道如何解决
models.py
class Person(SoftDeletionModel):
name = CharField(max_length=64)
email = CharField(blank=True, null=True, max_length=64)
contact_no = PositiveIntegerField()
address = CharField(max_length=256)
birth_date = DateField()
sex = CharField(max_length=1, choices=SEX)
photo = FileField(default='client/src/images/users.png')
class Driver(Person):
user = OneToOneField(User, on_delete=models.CASCADE, null=True)
application_date = DateField()
views.py
user_type = SignInView.get_user_type(user)
user_staff = SignInView.get_user_staff(user_type, user)
return Response(data={
"token": token.key,
"user": model_to_dict(user),
"user_type": user_type,
"user_staff": model_to_dict(user_staff)
}, status=200)
@staticmethod
def get_user_staff(user_type, user):
if user_type == "system_admin":
return user
if user_type == "driver":
return Driver.objects.get(user=user)
if user_type == "supervisor":
return Supervisor.objects.get(user=user)
if user_type == "operations_manager":
return OperationsManager.objects.get(user=user)
if user_type == "clerk":
return Clerk.objects.get(user=user)
我认为问题根源于django抛出错误,而不是如果照片为null则不返回None。有什么解决方法吗?请帮助
跟踪
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", line 158, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", line 156, in _get_response
response = response.render()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/template/response.py", line 106, in render
self.content = self.rendered_content
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rest_framework/response.py", line 72, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rest_framework/renderers.py", line 105, in render
allow_nan=not self.strict, separators=separators
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rest_framework/utils/json.py", line 28, in dumps
return json.dumps(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rest_framework/utils/encoders.py", line 67, in default
return tuple(item for item in obj)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rest_framework/utils/encoders.py", line 67, in <genexpr>
return tuple(item for item in obj)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/files/base.py", line 91, in __iter__
for chunk in self.chunks():
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/files/base.py", line 66, in chunks
self.seek(0)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/files/utils.py", line 20, in <lambda>
seek = property(lambda self: self.file.seek)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/files.py", line 41, in _get_file
self._require_file()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/files.py", line 38, in _require_file
raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
ValueError: The 'photo' attribute has no file associated with it.
答案 0 :(得分:0)
我通过在返回响应之前将驱动程序对象放入序列化器中来修复它,如下所示:
if user_type == "driver":
driver = DriverSerializer(Driver.objects.get(user=user))
return driver.data