我的数据库中有四个模型表:
详细信息:
class Block(models.Model):
name = models.CharField("block name", max_length=100)
desc = models.CharField("block description", max_length=100)
admin = models.CharField("block admin", max_length=100)
status = models.IntegerField(choices=STATUS)
class Article(models.Model):
tags = models.ManyToManyField(Tag, blank=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
block = models.ForeignKey(Block, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
content = models.TextField() # set the widget
status = models.IntegerField(choices=STATUS, default=1)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
class Comment(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
body = models.TextField() # set the widget
status = models.IntegerField(choices=STATUS)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
class Footnote(models.Model):
content = models.TextField()
article = models.ForeignKey(Article, blank=True, null=True, on_delete=models.CASCADE)
comment = models.ForeignKey(Comment, blank=True, null=True, on_delete=models.CASCADE)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
class Tag(models.Model):
owner = models.ForeignKey(User,on_delete=models.CASCADE)
name = models.CharField(max_length=50, unique=True)
我正在定义一个views.search以检索所有在其字段的每个角落包含搜索信息的文章
qualified_articles = (Article.objects
.filter(Q(owner__icontains=q) |
Q(block__icontains=q) |
Q(title__icontains=q) |
Q(content__icontains=q) |
Q(comment__body__icontains=q) |
Q(tag__name__icontains=q) |
Q(footnote__content__ficontains=(q)),
...
)
)
我手动列出了所有字段,
由于我正在查找有关Articles的所有内容,是否可以通过更像.filter(*__icontains=q)
之类的常规递归搜索来实现?