简体   繁体   English

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

[英]How to query for more than one content_type model from Django LogEntry?

I am able to query for one content type mode by doing this: 我可以通过执行以下操作来查询一种内容类型模式:

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

But what if I want all LogEntry objects that have content_type models foo and bar ? 但是,如果我要所有具有content_type模型foobar LogEntry对象,该怎么办?

I tried 我试过了

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

But that fails like so: 但是这样失败了:

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

Thanks 谢谢

You're super close, you just need to use a double underscore, like this: 您非常接近,您只需要使用双下划线,如下所示:

models_i_want = ['foo', 'bar']
LogEntry.objects.filter(content_type__model__in=models_i_want)
#                                           ^ this is the added _

See Django QuerySet docs for in Django的查询集文档in

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM