我想按content_type.name查询Django管理员的LogEntry记录。 我已经尝试过了:
from django.contrib.admin.models import LogEntry
logs = LogEntry.objects.filter(content_type__name='foo')
但这会导致以下错误跟踪:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/opt/sf/sf/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/sf/sf/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/sf/sf/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/sf/sf/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/sf/sf/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/sf/sf/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: name
我可以做到...
logs = LogEntry.objects.filter(content_type=102)
logs.all()[0].content_type.name
u'foo'
...那么如何获得所有名称为foo
的content_type对象?
答案 0 :(得分:2)
ContentType由三列id(整数),app_label(字符)和model(字符)组成
在这种情况下,您可能想要
LogEntry.objects.filter(content_type__model='foo')