推荐学习书目
› 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 · Nov 20, 2014 · 4741 views
    This topic created in 4336 days ago, the information mentioned may be changed or developed.
    上传的图片再删除对象后,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 replies  •  2014-11-20 12:57:06 +08:00
    lcqtdwj
        2
    lcqtdwj  
    OP
       Nov 20, 2014
    @Archangel_SDY 感谢,就是这个问题,解决了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2192 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 05:16 · PVG 13:16 · LAX 22:16 · JFK 01:16
    ♥ Do have faith in what you're doing.