Думаю ответ будет примерно такой
# model
class Bookmark(models.Model):
class Meta:
abstract = True
user = models.ForeignKey(User)
class BookmarkObject(Bookmark):
class Meta:
db_table = "bookmark_object"
obj = models.ForeignKey(Book,
null=True,
on_delete=models.CASCADE)
class BookmarkView(APIView):
model = BookmarkObject
permission_classes = [IsAuthenticated]
def post(self, request, pk):
# получаем пользователя
user = self.request.user.id
# получаем или создаём закладку
bookmark_object, created = self.model.objects.get_or_create(user=user,
obj_id=pk)
if not created:
bookmark_object.delete()
return Response(status.HTTP_201_CREATED)