A powerful and flexible API built using Django REST Framework (DRF), designed to support advanced product search capabilities with:
- Multilingual query support (English and Arabic)
- Tolerance for partial keywords and misspellings
- Efficient full-text search using PostgreSQL
- Full-text search powered by PostgreSQL
- Trigram similarity for fuzzy matching
- Case-insensitive filtering by category and brand
- Pagination support for scalable browsing
- Debugging tools integrated with
django-debug-toolbar
git clone https://github.com/FatMAnsour/Django-project-and-DRF-API.git
cd Django-project-and-DRF-APIpython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activateMake sure PostgreSQL is installed and accessible from the command line, then run:
pip install -r requirements.txtDownload and install PostgreSQL from the official website:
👉 https://www.postgresql.org/download/
After installation, make sure the PostgreSQL bin directory (e.g., C:\Program Files\PostgreSQL\17\bin) is added to your system's PATH.
Open your terminal or command prompt and run:
psql -U postgresThen within the PostgreSQL prompt:
CREATE DATABASE product_search_db;
\c product_search_db
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS unaccent;Replace
postgreswith your PostgreSQL username if different.
In product_search/settings.py, update the DATABASES section:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'product_search_db',
'USER': 'your_postgres_user',
'PASSWORD': 'your_postgres_password',
'HOST': 'localhost',
'PORT': '5432',
}
}Ensure the following are included in INSTALLED_APPS:
INSTALLED_APPS = [
...
'searchapp',
'django.contrib.postgres',
'debug_toolbar',
]python manage.py makemigrations
python manage.py migratepython manage.py createsuperuserpython manage.py runserver- API: http://localhost:8000
- Admin Panel: http://localhost:8000/admin/
- Debug Toolbar: http://localhost:8000/__debug__/
- URL:
/search/ - Method:
GET - Description: Search for products with optional filters for category and brand.
GET /search/?q=apple{
"count": 25,
"next": "http://localhost:8000/search/?q=apple&page=2",
"previous": null,
"results": [
{
"name": "Apple Juice",
"brand": "Nestle",
"category": "Beverages",
"nutrition_facts": "Calories: 120, Sugar: 25g"
},
{
"name": "Apple Pie",
"brand": "BakeryCo",
"category": "Desserts",
"nutrition_facts": "Calories: 300, Fat: 15g"
}
]
}GET /search/?q=choco&category=snacks{
"count": 10,
"next": null,
"previous": null,
"results": [
{
"name": "Chocolate Bar",
"brand": "Mars",
"category": "Snacks",
"nutrition_facts": "Calories: 200, Sugar: 20g"
}
]
}GET /search/{
"error": "Please enter a search query"
}- Pagination is supported using
nextandpreviousURLs. - Search supports misspellings, partial queries, and mixed languages (English and Arabic).
- Debug toolbar available at
/__debug__/when in development mode. - Populate
Product,Brand, andCategorymodels to view search results.