related Field has invalid lookup: icontains - WBowam/wbowam.github.com GitHub Wiki
Date: 2014-09-07
Title: related Field has invalid lookup: icontains 解决方法
Tags: Django
Category: It
这种错误常见于:
# 有一个表Category是表Article的category外键,Category有属性叫name,
class ArticleAdmin(admin.ModelAdmin):
"""
A simple AdminModel for Article.
"""
search_fields = ('title', 'category')
以上情况下肯定会报一个related Field has invalid lookup: icontains
错误
@classmethod
def get_articles(cls, CATEGORY=None, TAG=None, NUM=100):
"""
A simple classmethod.
Use Article.get_articles(CATEGORY=None, TAG=None, NUM=100) to get articles list.
"""
if CATEGORY:
article_list = cls.objects.filter(
Q(status=0) & Q(category__icontains=CATEGORY))[:NUM]
return article_list
return cls.objects.filter(status=0)[:NUM]
如上第二个Q查询里。
class ArticleAdmin(admin.ModelAdmin):
"""
A simple AdminModel for Article.
"""
search_fields = ('title', 'category__name')
@classmethod
def get_articles(cls, CATEGORY=None, TAG=None, NUM=100):
"""
A simple classmethod.
Use Article.get_articles(CATEGORY=None, TAG=None, NUM=100) to get articles list.
"""
if CATEGORY:
article_list = cls.objects.filter(
Q(status=0) & Q(category__name__icontains=CATEGORY))[:NUM]
return article_list
return cls.objects.filter(status=0)[:NUM]