2016-08-21 80 views
0

任何人都可以请建议我为什么我得到这个错误。'QuerySet'对象没有'filter_on_search'属性Python Django

与经理

class Customer(models.Model): 
    customer_id  = models.AutoField(primary_key=True) 
    customer_name = models.CharField(max_length=256) 
    customer_mobile = models.CharField(max_length=10) 
    customer_email = models.CharField(max_length=100,null=True) 
    customer_dob = models.DateField() 
    customer_remarks= models.CharField(max_length=256,null=True) 
    row_status  = models.BooleanField(choices=ROW_STATUS, default=True) 
    created_date = models.DateTimeField() 
    updated_date = models.DateTimeField() 

    objects   = MyClassManager() 

    def __unicode__(self): 
     return self.customer_name 

    def as_dict(self): 
     """ 
     Create data for datatables ajax call. 
     """ 
     return {'customer_name': self.customer_name, 
       'customer_mobile': self.customer_mobile, 
       'customer_dob': self.customer_dob, 
       } 

经理模式从这里开始以下是---------

class MyClassMixin(object): 
    def q_for_search_word(self, word): 
     """ 
     Given a word from the search text, return the Q object which you can filter on, 
     to show only objects containing this word. 
     Extend this in subclasses to include class-specific fields, if needed. 
     """ 
     return Q(name__icontains=word) | Q(supplier__name__icontains=word) 

    def q_for_search(self, search): 
     """ 
     Given the text from the search box, search on each word in this text. 
     Return a Q object which you can filter on, to show only those objects with _all_ the words present. 
     Do not expect to override/extend this in subclasses. 
     """ 
     q = Q() 
     if search: 
      searches = search.split() 
      for word in searches: 
       q = q & self.q_for_search_word(word) 
     return q 

    def filter_on_search(self, search): 
     """ 
     Return the objects containing the search terms. 
     Do not expect to override/extend this in subclasses. 
     """ 
     return self.filter(self.q_for_search(search)) 

class MyClassQuerySet(QuerySet, MyClassMixin): 
    pass 

class MyClassManager(models.Manager, MyClassMixin): 
    def get_query_set(self): 
     return MyClassQuerySet(self.model, using=self._db) 

这是我的看法-----

class MyAPI(JSONViewMixin, View): 
    "Return the JSON representation of the objects" 
    def get(self, request, *args, **kwargs): 

     class_name = kwargs.get('cls_name') 
     params = request.GET 
     # make this api general enough to handle different classes 
     klass = getattr(sys.modules['mudraapp.models'], class_name) 

     # TODO: this only pays attention to the first sorting column 
     sort_col_num = params.get('iSortCol_0', 0) 
     # default to value column 
     sort_col_name = params.get('mDataProp_{0}'.format(sort_col_num), 'value') 
     search_text = params.get('sSearch', '').lower() 
     sort_dir = params.get('sSortDir_0', 'asc') 
     start_num = int(params.get('iDisplayStart', 0)) 
     num = int(params.get('iDisplayLength', 25)) 
     obj_list = klass.objects.all() 
     sort_dir_prefix = (sort_dir=='desc' and '-' or '') 
     if sort_col_name in col_name_map: 
      sort_col = col_name_map[sort_col_name] 
      obj_list = obj_list.order_by('{0}{1}'.format(sort_dir_prefix, sort_col)) 

     filtered_obj_list = obj_list 
     if search_text: 
      filtered_obj_list = obj_list.filter_on_search(search_text) //Here I am getting error 

     d = {"iTotalRecords": obj_list.count(),    # num records before applying any filters 
      "iTotalDisplayRecords": filtered_obj_list.count(), # num records after applying filters 
      "sEcho":params.get('sEcho',1),      # unaltered from query 
      "aaData": [obj.as_dict() for obj in filtered_obj_list[start_num:(start_num+num)]] # the data 
     } 

     return self.json_response(d) 

我使用此代码的数据表分页和搜索分页工作良好,但同时搜索它给出了错误 我佛llowing下面的教程此 http://racingtadpole.com/blog/datatables-with-ajax-and-django/

回答

1

您在MyClassManager创建的方法有wrong name

def get_query_set(self): 

原本应该改为:

# query_set -> queryset 
def get_queryset(self): 
+0

谢谢,问题解决了。但是现在我收到了这个错误,您能否帮我解决 - FieldError at/Customer/ 无法将关键字'名称'解析为字段。选择是:created_date,customer_dob,customer_email,customer_id,customer_mobile,customer_name,customer_remarks,referance_id,row_status,updated_date –

+1

在'q_for_search_word'方法中将'name__icontains'更改为'customer_name__icontains'。您的“客户”模型中的字段称为“客户名称”,而不是“名称”。 – solarissmoke

+0

谢谢了! –

0

你对你的查询集使用filter_on_search。相反,您需要使用它来代替objects

所以,你会做

obj_list = klass.filter_on_search 

但是,看起来你可能希望将所有的逻辑移动从视图管理器出演。

对于它的价值,有一个真棒包,不正是你在数据表集成的Django想要的东西:https://pypi.python.org/pypi/django-datatables-view

相关问题