django filter字符串作为字符串

时间:2018-04-17 10:07:17

标签: django python-3.x

我想动态更改过滤字段。

我有一个名为Product的模型,字段为titlecode

class Product(models.Model):
    title = models.CharField(max_length=50)
    code = models.CharField(max_length=50)

我的过滤字段在此类视图中将是动态的

def filter(request):

    search_choices = {
        '1': 'title__icontains',
        '2': 'code__icontains',
        }

    col_num = request.GET.get("col_num")
    value = request.GET.get("value")

    search_field = search_choices.get("col_num")

    qs = Product.objects.filter(search_field=value)

    ........

这里变量search_field总是动态的...所以我怎样才能实现这个

1 个答案:

答案 0 :(得分:4)

您可以通过将参数作为字典传递来实现此目的:

const api = this.apiService.get(`/api/someEndpoint`);
const loading = Observable
  .timer(1000)
  .do(() => loading = true) // show loading
  .ignoreElements(); // or `filter(() => false)

Observable.merge(api, loading)
  .take(1)
  .subscribe(() => loading = false);