带有大型相关表的Django admin list_filter ForeignKey [英] Django admin list_filter ForeignKey with large related table

查看:83
本文介绍了带有大型相关表的Django admin list_filter ForeignKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个模型:

class Post(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField()
    user = models.ForeignKey('users.User')

这个ModelAdmin:

And this ModelAdmin:

class PostAdmin(admin.ModelAdmin):
    list_display = ['title', 'user']
    list_filter = ['user']

过滤效果很好,但是我遇到了这样的问题。在我的情况下, users.User 表非常大,因此当Django渲染 PostAdmin 页面时,需要太多时间呈现所有用户。

Filtering works well, but I faced with such problem. In my case users.User table is very big, so when Django is rendering PostAdmin page, it takes too many time to render all users.

所以我想知道是否可以在不渲染所有相关对象的情况下将Django Admin过滤器与ForeignKey一起使用,例如 raw_id_fields 用于过滤器的小部件?

So I am wondering if it is possible to use Django Admin filter with ForeignKey without rendering all related objects, something like raw_id_fields widget for filters?

推荐答案

我刚刚发现 https://github.com/lincolnloop/django-salmonella 看起来这将为任何FK提供原始ID过滤器。

I just found https://github.com/lincolnloop/django-salmonella looks like this will provide a raw id filter for any FK.

#pip install django-salmonella

from salmonella.admin import SalmonellaMixin
from salmonella.filters import SalmonellaFilter

class UserProfileAdmin(SalmonellaMixin, admin.ModelAdmin):
   list_filter = (
       ('salmonella_fk', SalmonellaFilter),
   )

您还可以清理 grapelli的样式 https://www.abidibo.net/blog/2015/02/06/pretty-raw_id_fields-django-salmonella-and-django-grappelli/

app / templates / salmonella / admin / widgets / salmonella_field.html

{{ hidden_input }}
<a onclick="return showRelatedObjectLookupPopup(this);" id="lookup_id_{{ name }}" data-name="{{ name }}" data-app="{{ app_name }}" data-model="{{ model_name }}" class="related-lookup" href="{{ related_url }}{{ url }}"><img width="16" height="16" alt="Consultazione" src="/static/admin/img/selector-search.gif"></a>
<a data-name="developers" data-app="{{ app_name }}" data-model="{{ model_name }}" class="salmonella-clear-field"></a>
<span class="salmonella_label" id="{{ name }}_salmonella_label"></span>

app / templates / salmonella / multi_label.html

{% for object in objects %}
    <a href="{{ object.1 }}" >{{ object.0 }}</a>{% if not forloop.last %} {% endif %}
{% endfor %}

和样式表:

.salmonella-clear-field {
    background-image: url("/static/grappelli/images/icons/related-remove.png");
    background-repeat: no-repeat;
    background-position: center center;
    display: inline-block;
    position: relative;
    top: 5px;
    margin: 0 5px 0 10px;
    height: 15px;
    width: 15px;
    cursor: pointer;
}
.salmonella_label {
    position: inline-block;
    position: relative;
    top: 2px;
}
.salmonella_label a {
    background: #fff;
    padding: 2px 5px;
}

这篇关于带有大型相关表的Django admin list_filter ForeignKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆