搜索django中的所有字段

时间:2019-02-12 16:46:34

标签: django search model

我的model.py中有5个表,所以我想用一个字符串搜索所有表,即用一个搜索字符串比较表中的所有字段。但是我找不到能够做到的方法。

class Person(models.Model):

    mail=models.EmailField()
    firstName=models.CharField(max_length=200)
    lastName=models.CharField(max_length=200)
    phoneNumber=PhoneNumberField()
    streetAdress=models.CharField(max_length=200)
    zipcode=models.CharField(max_length=200)
    city=models.CharField(max_length=200,default="Göteborg")
    country=models.CharField(max_length=200,default="Sweden")

    def __str__(self):
        return "%s %s" % (self.firstName,self.lastName)

    class Meta:
        ordering = ('firstName','lastName')

class Role(models.Model):

    role=models.CharField(max_length=200)
    person=models.ManyToManyField(Person)

    def __str__(self):
        return self.role

    class Meta:
        ordering = ('role',)

class Name(models.Model):

    name=models.CharField(max_length=200)
    role=models.ForeignKey(Role,on_delete=models.CASCADE)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ('name',)

class Address(models.Model):

    name=models.ForeignKey(Name,on_delete=models.CASCADE)
    venue=models.CharField(max_length=200,default="Company AB")
    street=models.CharField(max_length=200)
    city=models.CharField(max_length=200)
    mail=models.EmailField()
    phone=PhoneNumberField(default="")

    def __str__(self):
         return str(self.venue)

    # def __str__(self):
    #     return str(self.phone)

    class Meta:
        verbose_name_plural = "addreses"

    class Meta:
        ordering = ('name',)

class Date(models.Model):

    date=models.DateField()
    location=models.ManyToManyField(Address)

    def __str__(self):
         return str(self.date)

仅在名称字段中进行搜索:

class Search(ListView):
    print("class Search")
    model=Person
    template_name='artdb/search.html'
    context_object_name='ans'
#    Pdb().set_trace()

    def get_queryset(self):
        q=self.request.GET.get('seastr')
        if q:
            qans=firstName__icontains=q
            return Person.objects.filter(firstName__icontains=q)
        else:
            return Person.objects.all()  # SELECT ... WHERE headline ILIKE '%Lennon%';


    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['seastr'] = self.request.GET.get('seastr')
        return context

总有没有要搜索所有表和字段的信息?像通配符搜索?是否有与__icontains等效的通配符搜索?

1 个答案:

答案 0 :(得分:1)

从Django 1.10 SearchVector类开始添加了。

文档中的用法:

  

针对单个字段进行搜索非常棒,但却很局限。我们正在搜索的Entry实例属于一个Blog,该博客具有一个标语字段。要针对两个字段进行查询,请使用SearchVector

from django.contrib.postgres.search import SearchVector
Entry.objects.annotate(
...     search=SearchVector('body_text', 'blog__tagline'),
... ).filter(search='Cheese')
[<Entry: Cheese on Toast recipes>, <Entry: Pizza Recipes>]

另一种方法::您也可以按以下方式使用Q

from django.db.models import Q

class SearchAPI(APIView):
    def get(self, request, search_text, format=None, **kwargs):
        Model.objects.filter(Q(search_tags__contains=search_text) | Q(auto_tags__contains=search_text)