V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lcqtdwj
V2EX  ›  Python

求助,django,post_delete,partial

  •  
  •   lcqtdwj ·
    jiffies · 2014-11-20 09:17:42 +08:00 · 3926 次点击
    这是一个创建于 3444 天前的主题,其中的信息可能已经有所发展或是发生改变。
    上传的图片再删除对象后,django不会自动删除在media_root下的实际文件,所以用 post_delete.connect(delete_file,sender=ModelA) 但是我有2个模型都有imageField,并且他们的属性名不一样,所以我希望delete_file函数是通用的,接受一个指定属性名的参数,这样我就可以.

    from functools import partial post_delete.connect(partial(delete_file,'attr_name_A'),sender=ModelA) post_delete.connect(partial(delete_file,'attr_name_B'),sender=ModelB)

    def delete_file(attrname,sender,**kwargs): instance=kwargs['instance'] os.remove(os.path.join(BASE_DIR,instance.getattr[attrname].path))

    但是没有成功,没报任何异常错误,delete_file没有执行。请问哪里有问题么?或者应该怎样调试?
    2 条回复    2014-11-20 12:57:06 +08:00
    lcqtdwj
        2
    lcqtdwj  
    OP
       2014-11-20 12:57:06 +08:00
    @Archangel_SDY 感谢,就是这个问题,解决了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1863 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:28 · PVG 00:28 · LAX 09:28 · JFK 12:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.