Django Rest Framework-检索列表更新删除

vof42yt1  于 2023-10-21  发布在  Go
关注(0)|答案(1)|浏览(99)

我有这个类用于创建和更新课程内容。如何从这个视图转换或编写API视图?

class ContentCreateUpdateView(TemplateResponseMixin, View):
    module = None
    model = None
    obj = None
    template_name = 'courses/manage/content/form.html'

    def get_model(self, model_name):
        if model_name in ['text', 'video', 'image', 'file']:
            return apps.get_model(app_label='courses',
                                  model_name=model_name)
        return None

    def get_form(self, model, *args, **kwargs):
        Form = modelform_factory(model, exclude=['owner',
                                                 'order',
                                                 'created',
                                                 'updated'])
        return Form(*args, **kwargs)

    def dispatch(self, request, module_id, model_name, id=None):
        self.module = get_object_or_404(Module,
                                       id=module_id,
                                       course__owner=request.user)
        self.model = self.get_model(model_name)
        if id:
            self.obj = get_object_or_404(self.model,
                                         id=id,
                                         owner=request.user)
        return super().dispatch(request, module_id, model_name, id)

    def get(self, request, module_id, model_name, id=None):
        form = self.get_form(self.model, instance=self.obj)
        return self.render_to_response({'form': form,
                                        'object': self.obj})

    def post(self, request, module_id, model_name, id=None):
        form = self.get_form(self.model,
                             instance=self.obj,
                             data=request.POST,
                             files=request.FILES)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.owner = request.user
            obj.save()
            if not id:
                # new content
                Content.objects.create(module=self.module,
                                       item=obj)
            return redirect('module_content_list', self.module.id)
        return self.render_to_response({'form': form,
                                        'object': self.obj})

我想要wite API代码使用Django restframework我建立LMS使用Django我支持和React在前端问题是如何写API,如果我有这样的硬代码

8ftvxx2r

8ftvxx2r1#

你可以写你说的所有方法。首先是删除方法。

删除:

def delete(self, request, module_id, model_name, id=None):
    self.dispatch(self, request, module_id, model_name, id=id)
    self.obj.delete()
    return redirect('module_content_list')
    return self.render_to_response({f"{id}-object": "Deleted")

列表:

def list(self, request, module_id, model_name):
    self.dispatch(self, request, module_id, model_name)
    queryset_of_model = self.get_model().objects.all()
    template_name = "youwantfile.html"
    return self.render_to_response(template_name, {"model_list": queryset_of_model})

相关问题