Integration of RecomPI with Django models for recommendation and tracking functionalities.
Project description
Django RecomPI
Django RecomPI is a Django model mixin that integrates functionalities of RecomPI (Recommender System API) with Django models, enabling easy recommendation and tracking capabilities within Django applications.
Installation
You can install Django RecomPI via pip. Here's how:
pip install django-recompi
Quick Start
Setting up a Django model with RecomPI integration
- Define your Django model (e.g.,
Product
) and useRecomPIModelMixin
as a mixin.
from django.db import models
from django_recompi.models import RecomPIModelMixin
class Product(models.Model, RecomPIModelMixin):
RECOMPI_DATA_FIELDS = [
"name",
"reviews__comment",
"reviews__rating",
"reviews__counter.count",
]
name = models.CharField(max_length=100, db_index=True)
description = models.TextField()
def __str__(self):
return self.name
- Define related models such as
Review
andReviewCounter
with appropriate fields.
from django.db import models
class Review(models.Model):
product = models.ForeignKey(Product, related_name="reviews", on_delete=models.CASCADE)
comment = models.CharField(max_length=200, db_index=True)
rating = models.CharField(choices=RatingChoices.choices, max_length=1, db_index=True)
counter = models.ForeignKey("ReviewCounter", related_name="reviews", on_delete=models.CASCADE, default=None, null=True, blank=True)
def __str__(self):
return f"Review of {self.product.name} - {self.rating}"
class ReviewCounter(models.Model):
count = models.IntegerField(default=0)
Tracking interactions
# Track user interaction with a product
product = Product.objects.first()
# Using SecureProfile to hash profile identifiers before sending to RecomPI
profile = SecureProfile("profile_id", "user_profile_id")
# Providing location information including optional data
location = Location(
ip="1.1.1.1", # Optional: IP address of the user
url="https://www.example.com/products/1",
referer="REFERER_URL", # Optional: Referring URL
useragent="USERAGENT" # Optional: User agent of the client
)
# Tracking the interaction using RecomPI integration
product.recompi_track(
"product-view", # Interaction label
profile, # SecureProfile instance
location # Location instance
)
This revised version clarifies the usage of SecureProfile
and Location
classes while also providing a clear example of how to track user interactions with a product using the recompi_track
method. We encourage you to refer to the original RecomPI package documentation for detailed information on these classes and other useful utilities like Profile
.
Getting recommendations
# Get product recommendations for a user
recommendations = Product.recompi_recommend(
"product-view",
SecureProfile("profile_id", "user_profile_id"),
)
# Example of printing recommendations
print(
{
k: {"name": p.name, "recommedation-rank": p.recompi_rank}
for k, pp in recommendations.items()
for p in pp
}
)
Settings Configuration
Django RecomPI can be customized through the following settings in your settings.py
file, you can read the full documentation here; but the most important settings you much set in your settings.py
is RECOMPI_API_KEY
:
RECOMPI_API_KEY
- Type:
str
- Description: API key for accessing the RecomPI service. Required for integration.
- Note: To obtain
RECOMPI_API_KEY
, register on the RecomPI panel. After registration, add a campaign in the panel, and a campaign token will be generated instantly. Use this token as your API key in the code.
Security Considerations
Ensure the following security best practices when using Django RecomPI:
- Secure API Key Handling: Keep
RECOMPI_API_KEY
secure and avoid exposing it in version control or public repositories. - Data Encryption: Use HTTPS (
RECOMPI_SECURE_API
) to encrypt data transmitted between your Django application and the RecomPI service. - Secure Profile Hashing: Utilize
RECOMPI_SECURE_HASH_SALT
to hash profile IDs and other data obscuring before sending them to RecomPI servers. This helps protect user data by obscuring identifiable information during transmission and afterward.
Examples and Use Cases
Explore these examples to understand how Django RecomPI can be applied:
- E-commerce Recommendation: Track user interactions on product pages and recommend related products based on their behavior.
- Content Personalization: Customize content recommendations based on user preferences and historical interactions.
Advanced details
In this page you can find advanced topics on the django-recompi
package which can be very useful for developing sophisticated systems using the RecomPI recommendation system.
Contributing and Development
We welcome contributions to Django RecomPI! If you'd like to contribute, please follow these steps:
- Fork the repository and clone it to your local environment.
- Install dependencies and set up a development environment.
python3 -m venv venv
source venv/bin/activate
pip install -r requirements-dev.txt
pre-commit install --hook-type pre-commit --hook-type pre-push
- Make changes, write tests, and ensure all tests pass.
- Submit a pull request with a detailed description of your changes.
Support
For support or questions, please submit a ticket or open an issue on GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_recompi-2.1.1.tar.gz
.
File metadata
- Download URL: django_recompi-2.1.1.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cade3ec8d3b023df3fb07bf579c9333ea4dd164cdc65f5f47bf8eef0a068a477 |
|
MD5 | 4de2c0a47ac8de4cfebf0c3e30786c90 |
|
BLAKE2b-256 | b16aee8b8780f8c59a6bc3d53092e7b8de3992f58727d39f6d44b4b0ae36ca58 |
File details
Details for the file django_recompi-2.1.1-py3-none-any.whl
.
File metadata
- Download URL: django_recompi-2.1.1-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 745293d85abf407cbe2756f3e7aae20c4d632e07825a35acf049e5479be9a0f9 |
|
MD5 | 61b556970f4cb95ce2153277bb5d2530 |
|
BLAKE2b-256 | 43ba9e993a60c298a4d5f9205803a8c7e2e3479bb8e032a3008948c9386a293e |