组合 __startswith 和 __in 无效

combining __startswith and __in not working

我有一个 QuerySet 和一个字符串数组,我想针对 QuerySet 进行测试。 问题是我要检查的值是外键,外键的重要字符在开头。

我认为可行的查询是这样的:

materials_comparison_list.extend(materials_non_eu.filter(code__code__startswith__in=headings_list))

materials_non_eu是查询集,headings_list是数组

然而当运行它returns时出现以下错误:

django.core.exceptions.FieldError: Unsupported lookup 'startswith' for CharField or join on the field not permitted, perhaps you meant startswith or istartswith

我尝试更改位置或 __startswith 和 __in 但会产生相同的错误(不同的词)



材料模型如下所示:

class Materials(models.Model):
    id = models.AutoField(primary_key=True)
    row = models.IntegerField(null=True)
    code = models.ForeignKey('HS_code', on_delete=models.CASCADE, null=True)
    ...

代码的模型如下所示:

class HS_Code(models.Model):
    id = models.AutoField(primary_key=True)
    code = models.CharField(max_length=10, unique=False)
    ....

完整的控制台输出:

Traceback (most recent call last):
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
    raise exc
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/rest_framework/views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/rest_framework/decorators.py", line 50, in handler
    return func(*args, **kwargs)
  File "/Users/5knnbdwm/Python_env/FlexOrigin/main/user.py", line 1115, in api_user_summary_v2
    print(Calculation_Master(
  File "/Users/5knnbdwm/Python_env/FlexOrigin/main/cluster_v2.py", line 60, in Calculation_Master
    MAXNOM(session, materials, country, rule_block[1])
  File "/Users/5knnbdwm/Python_env/FlexOrigin/main/cluster_v2.py", line 140, in MAXNOM
    materials_comparison_list.extend(materials_non_eu.filter(
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/query.py", line 904, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/query.py", line 923, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1350, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1377, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1311, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1150, in build_lookup
    lhs = self.try_transform(lhs, name)
  File "/Users/5knnbdwm/Python_env/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1198, in try_transform
    raise FieldError(
django.core.exceptions.FieldError: Unsupported lookup 'startswith' for CharField or join on the field not permitted, perhaps you meant startswith or istartswith?

你不能把两者结合起来,但你可以用 Q object:

做一个析取过滤器
from django.db.models import Q

materials_comparison_list.extend(
    materials_non_eu.filter(Q(
        <b>*[('code__code__startswith', heading) for heading in headings_list]</b>,
        _connector=Q.OR
    ))
)