0

我需要在 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)

我收到一个 SuspiciousOperation 错误,这里有什么线索吗?

非常感谢你。

编辑:添加更多信息

这是 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.
4

1 回答 1

0

最后,问题是我的 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)

这就是我改变它的方式,这要归功于对这个问题的一些答案Suspicious Operation 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 定义末尾的斜线。

我不知道为什么它现在有效,我想我稍后会深入研究它

于 2013-06-07T18:17:37.367 回答