首页 > 解决方案 > Django CBV:在同一个类中创建函数

问题描述

我正在开发一个基于的新项目,Django Class Based View并且我想获得建议以分解重要的post function.

我对这个概念很陌生。

我的帖子功能如下所示:

def post(self, request, *args, **kwargs):
        form = self.form_class()
        query_document = None
        query_document_updated = None
        query_app = None
        query_document_count = None


    if "UpdateDocument" in request.POST:
        checkbox_id = request.POST['DocumentChoice']
        checkbox_id_minus_1 = int(checkbox_id) - 1

        query_document_updated = Document.objects.get(id=checkbox_id)

        APP_CODE = query_document_updated.app.code
        SRC_FILENAME = query_document_updated.src_filename
        FILENAME, file_extension = os.path.splitext(SRC_FILENAME)
        CATEGORY = query_document_updated.category

        if CATEGORY == "ANNUAL":
            CATEGORY = "ANNUAL_REPORT"

        # Get the new year selected by user
        year = self.request.POST.get('q1year')

        # Create the new document title updated by the new year
        new_document_title = f"{year}_{CATEGORY}_{APP_CODE}" + " - " + f"{SRC_FILENAME}"

        # Create the new document file updated by the new year
        new_document_file = "app_docs/" + f"{APP_CODE}" + "/" + \
                            f"{year}_{CATEGORY}_{APP_CODE}_{checkbox_id_minus_1}{file_extension}"

    context = {
        'form': form,
        'query_app' : query_app,
        'query_document': query_document,
        'query_document_updated': query_document_updated,
        'query_document_count' : query_document_count
    }
    return render(request, self.template_name, context)

我想创建两个新函数:new_document_titlenew_document_file调用这两个函数post

我怎么能这样做?在发布后创建两个函数并在参数中传递变量?

标签: djangodjango-class-based-views

解决方案


我认为您可以通过多种方式做到这一点!我在这里写几个,

方法一:使用class methods

from rest_framework.views import APIView


class MyView(APIView):
    def _new_document_title(self, *args, **kwargs):
        # do something
        return something

    def _new_document_file(self, *args, **kwargs):
        # do something
        return something

    def post(self, request, *args, **kwargs):
        self._new_document_title()  # calling "_new_document_title"
        self._new_document_file()  # _new_document_file
        # do something
        return some_response

方法2:使用Mixin class

class MyMixin(object):
    def new_document_title(self, *args, **kwargs):
        # do something
        return something

    def new_document_file(self, *args, **kwargs):
        # do something
        return something


class MyView(APIView, MyMixin):
    def post(self, request, *args, **kwargs):
        self.new_document_title()  # calling "new_document_title"
        self.new_document_file()  # new_document_file
        # do something
        return some_response

方法3:调用外部函数

from rest_framework.views import APIView


def new_document_title(self, *args, **kwargs):
    # do something
    return something


def new_document_file(self, *args, **kwargs):
    # do something
    return something


class MyView(APIView):

    def post(self, request, *args, **kwargs):
        new_document_title()  # calling "new_document_title"
        new_document_file()  # new_document_file
        # do something
        return some_response

推荐阅读