开发者

Django search in the database

开发者 https://www.devze.com 2023-03-20 04:46 出处:网络
I use MySQL for the production server and SQLite for the testing local server for my django application.

I use MySQL for the production server and SQLite for the testing local server for my django application.

I've a model like this:

class Product(models.Model):
    name = models.CharField(max_length = 40)

I've implemented a search feature in my application for searching product. I use this co开发者_Go百科mmand: Product.objects.filter(name__contains = text)

But in this way, if I search a white space " ", the application returns me every product with a space in the name. It's a bit ugly, and also if I search a single char, for example a it returns all name that contains a. Is there a way to search only 'single full word'?


https://docs.djangoproject.com/en/1.3/ref/models/querysets/#regex

http://docs.python.org/library/re.html

pattern= r"\b%s\b" % text
Product.objects.filter(name__regex = pattern)
0

精彩评论

暂无评论...
验证码 换一张
取 消