ValueError:“photo”属性没有与i关联的文件

2024-04-25 17:33:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我同时使用react和django,我似乎遇到了一个大问题。我在这个网站上找到的大多数解决方案都建议检查模板是否重复,但我没有使用django的默认模板引擎。当我使用model_to_dict(driver_object)时会出现此错误。驱动程序对象有一个照片,我在模型中设置为null=true。请帮帮我,我不知道怎么修理它

模型.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()

视图.py

^{pr2}$

我认为问题的根源是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.

Tags: djangoinpycoreselfresponselibpackages
1条回答
网友
1楼 · 发布于 2024-04-25 17:33:16

我通过在返回响应之前将驱动程序对象放在序列化程序中进行修复,如下所示:

    if user_type == "driver":
        driver = DriverSerializer(Driver.objects.get(user=user))
        return driver.data

相关问题 更多 >