任何人都可以請建議我爲什麼我得到這個錯誤。'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/
謝謝,問題解決了。但是現在我收到了這個錯誤,您能否幫我解決 - FieldError at/Customer/ 無法將關鍵字'名稱'解析爲字段。選擇是:created_date,customer_dob,customer_email,customer_id,customer_mobile,customer_name,customer_remarks,referance_id,row_status,updated_date –
在'q_for_search_word'方法中將'name__icontains'更改爲'customer_name__icontains'。您的「客戶」模型中的字段稱爲「客戶名稱」,而不是「名稱」。 – solarissmoke
謝謝了! –