使用Q对象时的Django NameError

时间:2016-07-10 07:08:22

标签: django

我使用Q对象作为搜索,

tracks_list = Track.objects.order_by("rating")
query = request.GET.get("search")

if query:
    tracks_list = tracks_list.filter(
            Q(title__icontains(query))|
            Q(genres__name__icontains(query))
    ).distinct()

我的模型被描述为,

class Track(models.Model):

  title = models.CharField(max_length=128)
  genres = SortedManyToManyField(Genre)
  rating = models.IntegerField(default=0, validators=[MaxValueValidator(10), MinValueValidator(0)])

class Genre(models.Model):
  name = models.CharField(max_length=128, unique=True, null='False')
  views = models.IntegerField(default=0)
  slug = models.SlugField(unique=True)

然而,我收到一条名为global name 'title__icontains' is not defined

的NameError

1 个答案:

答案 0 :(得分:3)

你需要

title__icontains(query)

而不是

genres__name__icontains

同样适用于{{1}}