2017-09-11 77 views
0

我有這樣的模式:Django管理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:

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

過濾效果很好,但我面臨着這樣的問題。在我的情況下,users.User表非常大,所以當Django渲染PostAdmin頁面時,渲染所有用戶需要太多時間。

所以我想知道是否有可能使用與外鍵的Django管理過濾器沒有呈現所有相關的對象,像raw_id_fields部件的過濾器?

+0

您正在尋找['limit_choices_to'](https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models .ForeignKey.limit_choices_to)。 –

+0

@BurhanKhalid謝謝你的回覆。但我需要有機會 選擇任何我想要過濾的用戶,沒有限制,所以'limit_choices_to'不是我的情況下的解決方案。 – neverwalkaloner

回答

1

我剛剛發現https://github.com/lincolnloop/django-salmonella看起來像這樣會爲任何FK提供原始ID過濾器。

# pip install django-salmonella

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

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

您還可以清理針對grapellihttps://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; 
} 
1

我能弄清楚的最佳解決方案是使用raw_id_field小部件自定義過濾器。

創建模板user_filter.html:在ModelAdmin

class UserFilter(admin.RelatedFieldListFilter): 
    """Raw_id_filter for user field""" 
    template = 'admin/user_filter.html' 

設置UserFilter作爲過濾器類用戶字段:

{% load static %} 
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/forms.css' %}"/> 
<script type="text/javascript">var go_from_select = function(opt) { window.location = window.location.pathname + opt };</script> 
<h3>{{ title }}</h3> 
<div class="admin-filter-user" style="margin-left:15px"> 
    <input type="text" style="display:inline-block;" name="{{title}}" value="{{ request.GET.user__id__exact }}" id="id_{{title}}" class="vForeignKeyRawIdAdminField"/> 
    <a href="{% url 'admin:users_user_changelist' %}" class="related-lookup" style="display:inline-block;" id="lookup_id_{{title}}" title="Lookup"></a> 
    <input type="button" onclick="go_from_select('?user__id__exact='+id_user.value)" value="Filter" /> 
    <a href="#" style="display:inline-block; margin-left:7px" onclick="go_from_select('')">clear</a> 
</div> 

創建新的過濾器類,並覆蓋默認模板

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

這將設置raw_id_field部件user過濾並提高頁面渲染性能。

相關問題