当试图读取django文件字段的默认值时,出现了可疑的操作错误



我需要在django中设置一个FileField模型,但是,如果用户没有给出一个值,我应该设置一个默认值。

然而,我的模型定义如下

class AutoAttendant(models.Model):
    name = models.CharField(max_length=32)
    t_menu_id = models.IntegerField()
    t_menu_prompt = models.FileField(upload_to='user_menus', default='%suser_menus/ringtu_default.mp3' % settings.MEDIA_URL, 
            blank=True, null=False)

我正在阅读和django阻止访问这个字段到任何其他路径,而不是在upload_to参数上指定的,所以我猜这应该是正确的

但是,然后,我需要读取此文件并将其发送到外部web服务,当我执行以下操作时:

send_to_ws(autoattendant_instance.t_menu_prompt.path)

我得到一个可疑操作错误,这里有任何线索吗?

非常感谢。

编辑:添加更多信息

send_to_ws的作用如下:

def add_menu(self, filepath, menu='ROOT'):
    method = self.service.set_menu_prompt
    f = open(filepath, 'rb')
    data = f.read()
    mime_type = 'audio/mpeg'
    bin_param = (data, mime_type)
    request = self.factory.create('ns0:SetMenuPromptRequest')
    request.i_menu = self.get_menu_id(menu)
    request.prompt_type = 'menu'
    request.prompt = f.name.split('/')[-1]
    response = attach(method, bin_param, ATT_EP, request)
    return response

在这个函数中,我构建了一个suds soap_request对象,并将文件附加为二进制附件。

我认为真正的问题是,我做一个打开(文件)直接在这个函数,有没有办法解决这个问题?

还有,这里是回溯

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/account/finish/
Django Version: 1.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'gunicorn',
 'django_extensions',
 'south',
 'compressor',
 'ringtu',
 'localization',
 'profiles',
 'services')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')

Traceback:
File "/home/israelord/.virtualenvs/ringtu-env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/israelord/.virtualenvs/ringtu-env/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  25.                 return view_func(request, *args, **kwargs)
File "/home/israelord/Work/RingTu/proyecto/code/profiles/views.py" in account_finish
  351.     upload = ph.add_menu(user)
File "/home/israelord/Work/RingTu/proyecto/code/profiles/helpers.py" in add_menu
  57.     response_upload = attwpr.add_menu(att)
File "/home/israelord/Work/RingTu/proyecto/code/services/decorators.py" in _wrap
  33.             result = f(*args, **kwargs)
File "/home/israelord/Work/RingTu/proyecto/code/services/autoattendant_wrapper.py" in add_menu
  111.         data = att_instance.t_menu_prompt.read()
File "/home/israelord/.virtualenvs/ringtu-env/local/lib/python2.7/site-packages/django/core/files/utils.py" in <lambda>
  16.     read = property(lambda self: self.file.read)
File "/home/israelord/.virtualenvs/ringtu-env/local/lib/python2.7/site-packages/django/db/models/fields/files.py" in _get_file
  46.             self._file = self.storage.open(self.name, 'rb')
File "/home/israelord/.virtualenvs/ringtu-env/local/lib/python2.7/site-packages/django/core/files/storage.py" in open
  36.         return self._open(name, mode)
File "/home/israelord/.virtualenvs/ringtu-env/local/lib/python2.7/site-packages/django/core/files/storage.py" in _open
  159.         return File(open(self.path(name), mode))
File "/home/israelord/.virtualenvs/ringtu-env/local/lib/python2.7/site-packages/django/core/files/storage.py" in path
  259.             raise SuspiciousOperation("Attempted access to '%s' denied." % name)
Exception Type: SuspiciousOperation at /account/finish/
Exception Value: Attempted access to '/media/user_menus/ringtu_default.mp3' denied.

最后,问题在于FileField()

上upload_to属性的定义

我一开始就是这么做的

class AutoAttendant(models.Model):
    name = models.CharField(max_length=32)
    t_menu_id = models.IntegerField()
    t_menu_prompt = models.FileField(upload_to='user_menus', default='%suser_menus/ringtu_default.mp3' % settings.MEDIA_URL, 
            blank=True, null=False)

这就是我如何改变它感谢这个问题的一些答案可疑操作Django

class AutoAttendant(models.Model):
    name = models.CharField(max_length=32)
    t_menu_id = models.IntegerField()
    t_menu_prompt = models.FileField(upload_to='user_menus/', default='%suser_menus/ringtu_default.mp3' % settings.MEDIA_URL, 
                                     blank=True, null=False)

注意upload_to定义末尾的斜杠。

我不知道为什么它现在可以工作,我想我以后会深入研究它

相关内容

  • 没有找到相关文章

最新更新