[英]Suspiciousoperation error when trying to read the default value of a django filefield
I need to set up a model with a FileField in django, but, if the user doesn't give a value, i should set a default. 我需要在Django中使用FileField设置模型,但是,如果用户未提供值,则应设置默认值。
However, my model definition is the following 但是,我的模型定义如下
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)
I was reading and django prevents access to this field to any other path rather than the specified on the upload_to parameter, so i guess this should be correct 我正在阅读,并且django禁止访问此字段的任何其他路径,而不是上载参数上指定的路径,所以我想这应该是正确的
But, then, I need to read this file and send it to an external web service and when i do the following: 但是,然后,当我执行以下操作时,我需要读取此文件并将其发送到外部Web服务:
send_to_ws(autoattendant_instance.t_menu_prompt.path)
i get a SuspiciousOperation error, any clue here? 我收到SuspiciousOperation错误,这里有任何线索吗?
Thank you very much. 非常感谢你。
EDIT: Adding more info 编辑:添加更多信息
Here is what send_to_ws does: 这是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
In this function i build a suds soap_request object and attach the file as a binary attachment. 在此函数中,我建立了一个肥皂肥皂对象,并将文件作为二进制附件附加。
I think the real problem is that I do an open(file) directly in this function, is there any way to work around this? 我认为真正的问题是我直接在此函数中执行了一个open(file),是否有任何方法可以解决此问题?
also, here is the traceback 还有,这是回溯
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.
In the end, the problem was the definition of the upload_to attribute on my FileField() 最后,问题是我FileField()上的upload_to属性的定义
This was how i did it at first 一开始就是这样
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)
And this is how i changed it thanks to some answers to this question Suspicious Operation Django 这是我改变了它由于一些这个问题的答案可疑交易的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)
notice the slash in the end of the upload_to definition. 注意upload_to定义末尾的斜杠。
I have no idea why it works now, i guess I'll dig into it later 我不知道为什么现在可以使用,我想稍后再深入研究
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.