如何从Django LogEntry查询多个content_type模型?

时间:2018-10-18 20:21:50

标签: python django

我可以通过以下方式查询一种内容类型模式:

LogEntry.objects.filter(content_type__model='foo')

但是,如果我要所有具有content_type模型foobar的LogEntry对象,该怎么办?

我尝试了

models_i_want = ['foo', 'bar']
LogEntry.objects.filter(content_type__model_in=models_i_want)

但是那样失败:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/opt/sfo/sfo/env/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/opt/sfo/sfo/env/lib/python2.7/site-packages/django/db/models/query.py", line 781, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/opt/sfo/sfo/env/lib/python2.7/site-packages/django/db/models/query.py", line 799, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/opt/sfo/sfo/env/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1260, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/opt/sfo/sfo/env/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1286, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/opt/sfo/sfo/env/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1211, in build_filter
    raise FieldError('Related Field got invalid lookup: {}'.format(lookups[0]))
FieldError: Related Field got invalid lookup: model_in

谢谢

1 个答案:

答案 0 :(得分:2)

您超级亲密,您只需要使用双下划线,如下所示:

AsyncHttpClient http = asyncHttpClient(config()
    .setMaxConnections(500)
    .setMaxConnectionsPerHost(200)
    .setPooledConnectionIdleTimeout(100)
    .setConnectionTtl(500)
);

有关models_i_want = ['foo', 'bar'] LogEntry.objects.filter(content_type__model__in=models_i_want) # ^ this is the added _ ,请参见Django QuerySet docs