mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 12:51:09 -05:00
19 lines
726 B
Python
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() |