admin list_display上的django反向外键失败

时间:2017-01-20 21:49:28

标签: django-admin

我想在admin list_change上显示反向外键:

# models.py
class Person(models.Model):
    id = models.CharField(max_length=50, primary_key=True)

class Book(models.Model):
    person = models.ForeignKey(Person, related_name='samples')
    name = models.CharField(max_length=50)

#admin.py
@admin.register(Person)
class PersonAdmin(CustomAdmin):
    list_display = ('id', 'links')

    def links(self, obj):
        links = obj.book_set().all()
        return mark_safe('<br/>'.join(links))

就像this other post

我使用django 1.8但它失败了: 堆栈跟踪:

  File "/venv/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 320, in result_list
    'results': list(results(cl))}
  File "/venv/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 296, in results
    yield ResultList(None, items_for_result(cl, res, None))
  File "/venv/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 287, in __init__
    super(ResultList, self).__init__(*items)
  File "/venv/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 199, in items_for_result
    f, attr, value = lookup_field(field_name, result, cl.model_admin)
  File "/venv/lib/python2.7/site-packages/django/contrib/admin/utils.py", line 278, in lookup_field
    value = attr(obj)
  File "/home/kparsa/boom/myapp/myapp/admin.py", line 299, in links
    link = obj.book_set().all()
  File "/venv/lib/python2.7/site-packages/django/db/models/fields/related.py", line 691, in __call__
    manager = getattr(self.model, kwargs.pop('manager'))
KeyError: u'manager'

有谁知道如何让它正常工作? 请注意,我不想做像

这样的事情
qres = Book.objects.filter(person__id=obj.id).values_list('id', 'name').order_by('name')
for x, y in qres:
    links.append('<a href="/admin/mypp/book?q=ID{}">{}</a>'\
        .format(x, y))

因为它会运行许多重复的查询(行数)。

1 个答案:

答案 0 :(得分:0)

错误是因为我在模型中使用了related_name,但我试图使用默认的模型名称。 为了解决重复查询的问题,我查询了get_queryset()中的所有结果,将结果存储在memcached中,然后存储在&#34;链接&#34;方法,我只是从缓存中取出它。 效果很好! 这里唯一的危险是在推送新数据时失去同步。我在缓存上放置了100秒的超时以避免问题。