python - 在过滤器中使用 django 查询集中的变量或清空过滤器

标签 python django django-queryset

问题 1. 如何在此查询集中使用变量,

def function(request):
    qset1 = BigTable.objects.values(
        'deb_nr_f__dcountry__wcode_f__code',
        'deb_nr_f__dcountry__wcode_f__code_name',
        ).filter(id_nr_f__ie_code__unomer_f__nomer=iteration_result,
        )

我的意思是类似的事情

def function(request):

    x1 = 'id_nr_f__ie_code__unomer_f__nomer'

    qset1 = BigTable.objects.values(
        'deb_nr_f__dcountry__wcode_f__code',
        'deb_nr_f__dcountry__wcode_f__code_name',
        ).filter(x1 = iteration_result,
        )

问题2。 如何使用变量清空过滤器,以便查询中的过滤器不过滤记录。我的意思是类似的事情

def function(request):

    x1 = 'id_nr_f__ie_code__unomer_f__nomer'
    x2 = True

    qset1 = BigTable.objects.values(
        'deb_nr_f__dcountry__wcode_f__code',
        'deb_nr_f__dcountry__wcode_f__code_name',
        ).filter(x1 = x2,
        )

谢谢。

最佳答案

您可以定义一个dict,然后将其作为kwargs传递给filter

attr1 = 'some attr'
attr2 = 'some attr'
filter_dict = {
    attr1:filter1,
    attr2:filter2,
     ...
}
queryset.filter(**filter_dict)

关于python - 在过滤器中使用 django 查询集中的变量或清空过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60936810/

相关文章:

python - 在没有 virtualenv 的情况下启动 Celery

python - 为什么关系中的重复项不违反 UniqueConstraint?

python - 添加 "status light"到 Django admin

django - 批量更新表

python - 如何通过过滤的桥接表字段对查询集进行排序?

python - 沿轴平滑数组

python - 数据框打印不正确

Django /GAE : How to filter query set on foreignkey?

django - 筛选相关模型django

django - 如何在 Django 中打印单个对象的所有值?