Package: recommendation-68911
Recommendation_68911Django PyPi package serving as the interface for the recommendation system, providing models, managers, and migrations required by the vector recommendation service. This package is meant only for Django version 3.2 (and above) and above using the PostgreSQL database version 9.5 (and above). The package is intended to be used with a specific recommendation engine developed as part of my master's thesis project (not yet published) and is not for general purposes.Installation guideInstall pip package:pipinstallrecommendation-68911Add the package to the installed apps in thesettings.pyfile (recommended before custom apps depending on this package).# settings.pyINSTALLED_APPS=[...'recommendation_68911','your_custom_app',]Add theRECOMMENDATION_SQS_URLvariable to your settings, which will specify the URL of your recommendation microservice API endpoint.# settigs.pyRECOMMENDATION_SQS_URL='https://your.api.endpiont/...'Run the pre-created migration to install the cube extension for your PostgreSQL (extension is compatible to be used also within AWS RDS).python3manage.pymigraterecommendation_68911Add RecomObjectQuerySet as a parent to your recommended object class QuerySet manager. If you don't have one, you will need to create one.# managers.pyfromrecommendation_68911.managersimportRecomObjectQuerySetclassYourObjectQuerySet(...,RecomObjectQuerySet):...SpecifyAbstractRecomObjectas the parent of the object class you want to recommend andAbstractRecomActorfor the actor class in your app'smodels.py. Also specify objects attributable toYourObjectClassto use theQuerySetManagercreated in the previous step. Finally, it's strongly recommended to specify cases when you don't want to perform object vector updates in thesave()method by setting theself.update_vectorattribute. If updating the vector is wanted, it is required to provide the object's attributes inself.dict_repr.# models.pyfromrecommendation_68911.modelsimportAbstractRecomObject,AbstractRecomActorclassYourObjectModel(...,AbstractRecomObject):...objects=YourObjectManager.from_queryset(YourObjectQuerySet)...defsave(self,*args,**kwargs):ifnotyour_condition:self.update_vector=Falseelse:self.dict_repr={...}super().save(*args,**kwargs)classYourActorModel(...,AbstractRecomActor):...After this step, you are ready to create and execute the migration to apply changes.python3manage.pymakemigrations<your_app>
python3manage.pymigrate<your_app>To send your actor-object interactions to the microservice, you'll need to use thecreate_interaction()method of the providedRecommendationServiceclass. It's recommended to perform this task within thedispatch()method of your view.# views.pyfromrecommendation_68911.servicesimportRecommendationServiceclassYourDetailView(...):...defdispatch(self,request,*args,**kwargs):super_result=super().dispatch(request,*args,**kwargs)...ifyour_sessionandyour_object.vector!=None:RecommendationService().create_interaction(object_dict={...},object_vector=your_object.vector,object_type="YOUR_TYPE",your_session.session_keyifyour_sessionelseNone,your_actorifyour_actor.is_authenticatedelseNone)returnsuper_resultYour integration of the recommendation system is now finished. To apply a recommendation filter to a query set, add theorder_by_recommended()method at the end of your query.# views.pyclassYourListView(...):...defget_queryset(...):qs=super().get_queryset(...)...returnqs.order_by_recommended(your_actor_object.vector)