使用 flaggit 定义 Django Rest Framework 视图
Define Django Rest Framework view with flaggit
我正在尝试使用这个 Django 应用程序 https://github.com/TheBimHub/django-flaggit
我已经通过 pip 安装了 flaggit。然后在 views.py 我有
import flaggit
def flag_thread(request, **kwargs):
thread_id = request.GET.get('thread_id')
thread = Thread.objects.find(id=thread_id)
flaggit.utils.flag(thread, user=None, ip=None, comment=None)
这是正确的吗? urls.py 是什么样子的?
首先做一个序列化器
from flaggit.models import FlagInstance
...
class FlagInstanceSerializer(serializers.ModelSerializer):
class Meta:
model = FlagInstance
fields = '__all__'
然后在新端点中使用序列化程序
class ThreadViewSet(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated,)
queryset = Thread.objects.all().order_by('-created_at')
pagination_class = ThreadViewSetPaginationClass
@action(detail=True, methods=['post'])
def flag(self, request, pk=None):
thread = self.get_object()
flag_instance = flaggit.utils.flag(thread, user=request.user, ip=None, comment=None)
serializer = FlagInstanceSerializer(data=flag_instance)
serializer.is_valid()
return Response(serializer.data)
我继续修复了 fork 中的迁移和管理面板 https://github.com/morenoh149/django-flaggit
我正在尝试使用这个 Django 应用程序 https://github.com/TheBimHub/django-flaggit
我已经通过 pip 安装了 flaggit。然后在 views.py 我有
import flaggit
def flag_thread(request, **kwargs):
thread_id = request.GET.get('thread_id')
thread = Thread.objects.find(id=thread_id)
flaggit.utils.flag(thread, user=None, ip=None, comment=None)
这是正确的吗? urls.py 是什么样子的?
首先做一个序列化器
from flaggit.models import FlagInstance
...
class FlagInstanceSerializer(serializers.ModelSerializer):
class Meta:
model = FlagInstance
fields = '__all__'
然后在新端点中使用序列化程序
class ThreadViewSet(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated,)
queryset = Thread.objects.all().order_by('-created_at')
pagination_class = ThreadViewSetPaginationClass
@action(detail=True, methods=['post'])
def flag(self, request, pk=None):
thread = self.get_object()
flag_instance = flaggit.utils.flag(thread, user=request.user, ip=None, comment=None)
serializer = FlagInstanceSerializer(data=flag_instance)
serializer.is_valid()
return Response(serializer.data)
我继续修复了 fork 中的迁移和管理面板 https://github.com/morenoh149/django-flaggit