MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

filter admin django dan mongodb:Tertangkap DatabaseError saat merender:Kueri ini tidak didukung oleh database

Saya seorang pemula Python/Django, tetapi saya berhasil memperbaikinya dengan menyalin AllValuesFieldListFilter dari https://github.com/django/django/blob/stable/1.4.x/django/contrib/admin/filters.py (pastikan untuk mengubah cabang Anda ke versi Django Anda) dan kemudian saya menghapus panggilan ke distinct . Harus mengimpor banyak barang. Kemudian bekerja. Saya juga menerapkan perubahan dari jawaban @ AlexeyMK untuk membuatnya berbeda lagi.

untuk 1.4:

from django.contrib.admin.filters import FieldListFilter
from django.contrib.admin.util import (get_model_from_relation, reverse_field_path, get_limit_choices_to_from_path, prepare_lookup_value)
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_unicode, force_unicode

class MongoFieldListFilter(FieldListFilter):
def __init__(self, field, request, params, model, model_admin, field_path):
    self.lookup_kwarg = field_path
    self.lookup_kwarg_isnull = '%s__isnull' % field_path
    self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull,
                                             None)
    parent_model, reverse_path = reverse_field_path(model, field_path)
    queryset = parent_model._default_manager.all()
    # optional feature: limit choices base on existing relationships
    # queryset = queryset.complex_filter(
    #    {'%s__isnull' % reverse_path: False})
    limit_choices_to = get_limit_choices_to_from_path(model, field_path)
    queryset = queryset.filter(limit_choices_to)

    def uniquify(coll):  # enforce uniqueness, preserve order
      seen = set()
      return [x for x in coll if x not in seen and not seen.add(x)]

    self.lookup_choices = uniquify(queryset.order_by(field.name).values_list(field.name, flat=True))

    super(MongoFieldListFilter, self).__init__(field, request, params, model, model_admin, field_path)

def expected_parameters(self):
    return [self.lookup_kwarg, self.lookup_kwarg_isnull]

def choices(self, cl):
    from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
    yield {
        'selected': (self.lookup_val is None
            and self.lookup_val_isnull is None),
        'query_string': cl.get_query_string({},
            [self.lookup_kwarg, self.lookup_kwarg_isnull]),
        'display': _('All'),
    }
    include_none = False
    for val in self.lookup_choices:
        if val is None:
            include_none = True
            continue
        val = smart_unicode(val)
        yield {
            'selected': self.lookup_val == val,
            'query_string': cl.get_query_string({
                self.lookup_kwarg: val,
            }, [self.lookup_kwarg_isnull]),
            'display': val,
        }
    if include_none:
        yield {
            'selected': bool(self.lookup_val_isnull),
            'query_string': cl.get_query_string({
                self.lookup_kwarg_isnull: 'True',
            }, [self.lookup_kwarg]),
            'display': EMPTY_CHANGELIST_VALUE,
        }

Kemudian tentukan untuk menggunakan filter ini seperti:

list_filter = (('myfield', MongoFieldListFilter),)

Ini bagus karena tidak perlu menambal.



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. hentikan mongodb membuat dbs dan koleksi secara dinamis

  2. Luwak menghapus (menarik) dokumen dalam array, tidak berfungsi dengan ObjectID

  3. mongodb db.collection.find({}) tidak melewati .limit(101)

  4. SailsJS:Dapatkan Resonse dari Ajax-Form of SailsJS dalam fungsi yang dikirimkan

  5. Persimpangan indeks vs indeks gabungan?