• <small id='Qr4SI'></small><noframes id='Qr4SI'>

      <tfoot id='Qr4SI'></tfoot>
        <bdo id='Qr4SI'></bdo><ul id='Qr4SI'></ul>

      <legend id='Qr4SI'><style id='Qr4SI'><dir id='Qr4SI'><q id='Qr4SI'></q></dir></style></legend>
      <i id='Qr4SI'><tr id='Qr4SI'><dt id='Qr4SI'><q id='Qr4SI'><span id='Qr4SI'><b id='Qr4SI'><form id='Qr4SI'><ins id='Qr4SI'></ins><ul id='Qr4SI'></ul><sub id='Qr4SI'></sub></form><legend id='Qr4SI'></legend><bdo id='Qr4SI'><pre id='Qr4SI'><center id='Qr4SI'></center></pre></bdo></b><th id='Qr4SI'></th></span></q></dt></tr></i><div id='Qr4SI'><tfoot id='Qr4SI'></tfoot><dl id='Qr4SI'><fieldset id='Qr4SI'></fieldset></dl></div>
      1. 按 Null/Is Not Null 过滤 Django Admin

        时间:2023-11-08
        <i id='ya7XF'><tr id='ya7XF'><dt id='ya7XF'><q id='ya7XF'><span id='ya7XF'><b id='ya7XF'><form id='ya7XF'><ins id='ya7XF'></ins><ul id='ya7XF'></ul><sub id='ya7XF'></sub></form><legend id='ya7XF'></legend><bdo id='ya7XF'><pre id='ya7XF'><center id='ya7XF'></center></pre></bdo></b><th id='ya7XF'></th></span></q></dt></tr></i><div id='ya7XF'><tfoot id='ya7XF'></tfoot><dl id='ya7XF'><fieldset id='ya7XF'></fieldset></dl></div>

          1. <legend id='ya7XF'><style id='ya7XF'><dir id='ya7XF'><q id='ya7XF'></q></dir></style></legend>

              <bdo id='ya7XF'></bdo><ul id='ya7XF'></ul>

                <small id='ya7XF'></small><noframes id='ya7XF'>

                • <tfoot id='ya7XF'></tfoot>
                    <tbody id='ya7XF'></tbody>
                  本文介绍了按 Null/Is Not Null 过滤 Django Admin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个简单的 Django 模型,例如:

                  I have a simple Django model like:

                  class Person(models.Model):
                      referrer = models.ForeignKey('self', null=True)
                      ...
                  

                  在这个模型的 ModelAdmin 中,我如何允许它通过referrer 是否为空来过滤?默认情况下,向 list_filter 添加推荐人会导致显示一个下拉列表,其中列出了 每个 人的记录,可能有数十万条记录,从而有效地阻止了页面加载.即使加载,我仍然无法按我想要的条件进行过滤.

                  In this model's ModelAdmin, how would I allow it to be filtered by whether or not referrer is null? By default, adding referrer to list_filter causes a dropdown to be shown that lists every person record, which may be in the hundreds of thousands, effectively preventing the page from loading. Even if it loads, I still can't filter by the criteria I want.

                  即我将如何修改它以使下拉列表仅列出All"、Null"或Not Null"选项?

                  i.e. How would I modify this so that the dropdown only lists "All", "Null", or "Not Null" choices?

                  我看到一些 帖子 声称使用自定义 FilterSpec 子类完成类似的事情,但没有一个解释如何使用它们.我见过的少数似乎适用于所有模型中的所有领域,这是我不想要的.此外,FilterSpec 的文档,这让我很紧张,因为我不想投资大量与某些可能在下一个版本中消失的临时内部类相关的自定义代码.p>

                  I've seen some posts that claim to accomplish something similar using custom FilterSpec subclasses, but none of them explain how to use them. The few I've seen appear to apply to all fields in all models, which I wouldn't want. Moreover, there's zero documentation for FilterSpec, which makes me nervous, because I don't want to invest in a lot of custom code tied to some transient internal class that might disappear by the next release.

                  推荐答案

                  我最终使用了 这里的最佳解决方案,以及这个片段.

                  I ended up using a mixture of the top solution here, along with this snippet.

                  但是,我不得不稍微调整一下代码片段,删除字段类型限制并添加最近在 1.3 中添加的新 field_path.

                  However, I had to tweak the snippet slightly, dropping the field type restriction and adding the new field_path, recently added in 1.3.

                  from django.contrib.admin.filterspecs import FilterSpec
                  from django.db import models
                  from django.utils.safestring import mark_safe
                  from django.utils.translation import ugettext as _
                  
                  class NullFilterSpec(FilterSpec):
                      #fields = (models.CharField, models.IntegerField, models.FileField)
                  
                      @classmethod
                      def test(cls, field):
                          #return field.null and isinstance(field, cls.fields) and not field._choices
                          return field.null and not field._choices
                      #test = classmethod(test)
                  
                      def __init__(self, f, request, params, model, model_admin, field_path=None):
                          super(NullFilterSpec, self).__init__(f, request, params, model, model_admin, field_path)
                          self.lookup_kwarg = '%s__isnull' % f.name
                          self.lookup_val = request.GET.get(self.lookup_kwarg, None)
                  
                      def choices(self, cl):
                          # bool(v) must be False for IS NOT NULL and True for IS NULL, but can only be a string
                          for k, v in ((_('All'), None), (_('Has value'), ''), (_('Omitted'), '1')):
                              yield {
                                  'selected' : self.lookup_val == v,
                                  'query_string' : cl.get_query_string({self.lookup_kwarg : v}),
                                  'display' : k
                              }
                  
                  # Here, we insert the new FilterSpec at the first position, to be sure
                  # it gets picked up before any other
                  FilterSpec.filter_specs.insert(0,
                      # If the field has a `profilecountry_filter` attribute set to True
                      # the this FilterSpec will be used
                      (lambda f: getattr(f, 'isnull_filter', False), NullFilterSpec)
                  )
                  

                  这篇关于按 Null/Is Not Null 过滤 Django Admin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 Django admin 的自定义列 下一篇:Django:StackedInline 的更友好的标题,用于通过模型自动生成?

                  相关文章

                  <i id='nlkxj'><tr id='nlkxj'><dt id='nlkxj'><q id='nlkxj'><span id='nlkxj'><b id='nlkxj'><form id='nlkxj'><ins id='nlkxj'></ins><ul id='nlkxj'></ul><sub id='nlkxj'></sub></form><legend id='nlkxj'></legend><bdo id='nlkxj'><pre id='nlkxj'><center id='nlkxj'></center></pre></bdo></b><th id='nlkxj'></th></span></q></dt></tr></i><div id='nlkxj'><tfoot id='nlkxj'></tfoot><dl id='nlkxj'><fieldset id='nlkxj'></fieldset></dl></div>
                    <tfoot id='nlkxj'></tfoot>
                      • <bdo id='nlkxj'></bdo><ul id='nlkxj'></ul>

                      <legend id='nlkxj'><style id='nlkxj'><dir id='nlkxj'><q id='nlkxj'></q></dir></style></legend>

                      <small id='nlkxj'></small><noframes id='nlkxj'>