shanshuise
V2EX  ›  Django

请教个关于 django filter 的简单问题。

  •  
  •   shanshuise · Sep 22, 2014 · 3431 views
    This topic created in 4330 days ago, the information mentioned may be changed or developed.
    一般django取数据的时候所用的是类似这种Entry.objects.filter(name=4)的写法,name是models.py里的模型的属性。


    但是模型的属性有许多个,我想做一个最简单的搜索,要搜索的属性与该属性的值都来自html中提交。也就是用户向搜索哪个属性自己在html页面里边选择好。

    但是在views.py里用request.POST.get("")来获取到的是字符串,不知道该怎么写view才能将上边第一段里边的name换成用户选择的属性?
    3 replies    2014-09-24 10:58:36 +08:00
    wangyongbo
        2
    wangyongbo  
       Sep 22, 2014   ❤️ 1
    python 里面可以使用** 传入字典参数。
    def f(**kwargs):
    for k, v in kwargs.items():
    print k, v

    你可以把 传上来的参数组合为一个字典 Entry.objects.filter(**kwargs)
    shanshuise
        3
    shanshuise  
    OP
       Sep 24, 2014
    @wangyongbo 最后使用的是这个方法。实测可行,多谢。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3016 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 03:40 · PVG 11:40 · LAX 20:40 · JFK 23:40
    ♥ Do have faith in what you're doing.