Files
thrillwiki_django_no_react/reviews/mixins.py

19 lines
726 B
Python

from django.contrib.contenttypes.models import ContentType
from django.db.models import QuerySet
class ReviewableMixin:
"""Mixin for models that can have reviews."""
def get_reviews(self) -> QuerySet:
"""Get reviews for this instance."""
from reviews.models import Review
ct = ContentType.objects.get_for_model(self.__class__)
return Review.objects.filter(content_type=ct, object_id=self.pk)
def add_review(self, review: 'Review') -> None:
"""Add a review to this instance."""
from reviews.models import Review
ct = ContentType.objects.get_for_model(self.__class__)
review.content_type = ct
review.object_id = self.pk
review.save()