class CreateProjectWorkorderView(views.APIView):
    permission_classes = [permissions.IsAuthenticated]

    def post(self, request, project_title):
        user = request.user
        try:
            project_sites = ProjectSiteModel.objects.filter(title=project_title)
        except Exception as e:
            return response.Response({"detail": "Project title not found"}, status=status.HTTP_404_NOT_FOUND)

        try:
            project_tickets = ProjectWorkorderModel.objects.filter(project_title=project_title)
            if project_tickets.exists():
                return response.Response({"detail": "Project tickets already exist"},
                                         status=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            return response.Response({"detail": "Error while filtering project ticket model"},
                                     status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        if project_sites.exists():
            data_list = project_sites.values_list('data', flat=True)
            cpg_data_list = project_sites.values_list('cpg_data', flat=True)

            work_orders_to_create = {
                'SA': [],
                'ROAD': [],
                'POWER': [],
                'TI': [],
                'CW': [],
            }

            for data, cpg_data in zip(data_list, cpg_data_list):
                data_dict = json.loads(data)
                cpg_data_dict = json.loads(cpg_data)

                for key in cpg_data_dict:
                    if key in data_dict:
                        data_dict[key] = cpg_data_dict[key]

                site_title = data_dict.get('Site ID')
                pm = data_dict.get('MTNI PM/PC')
                parent_work_order_id = create_parent_work_order_id(site_title, project_title)
                project_ticket = ProjectWorkorderModel.objects.create(
                    site_title=site_title,
                    status='pending',
                    created_by=user,
                    pm=pm,
                    work_order_id=parent_work_order_id,
                    project_title=project_title,
                    data=data_dict
                )

                contractors = {
                    'SA Contractor Name': 'SA',
                    'CW Contractor Name': 'CW',
                    'TI Contractor Name': 'TI',
                    'Road Contractor Name': 'ROAD',
                    'Power Contractor Name': 'POWER',
                }

                for contractor_type, domain in contractors.items():
                    contractor_name = data_dict.get(contractor_type)
                    company = None
                    extra_contractor = None

                    if contractor_name in ['TBD', 'NTBD']:
                        extra_contractor = contractor_name
                        if contractor_name == 'NTBD':
                            continue
                    else:
                        try:
                            company = Company.objects.get(name=contractor_name)
                        except Company.DoesNotExist:
                            return response.Response({"detail": f"Contractor name {contractor_name} not found"},
                                                     status=status.HTTP_404_NOT_FOUND)

                    work_order_id = create_contractor_work_order_id(domain, site_title, project_title)

                    work_order_model_class = {
                        'SA': SAWorkOrdersModel,
                        'CW': CWWorkOrdersModel,
                        'TI': TIWorkOrdersModel,
                        'ROAD': RoadWorkOrdersModel,
                        'POWER': PowerWorkOrdersModel,
                    }[domain]
                    if contractor_name == 'TBD':
                        wo_status = 'TBD'
                    else:
                        if domain == 'SA':
                            wo_status = 'pending'
                        else:
                            wo_status = 'locked'
                    work_orders_to_create[domain].append(
                        work_order_model_class(
                            parent_work_order=project_ticket,
                            work_order_id=work_order_id,
                            contractor=company,
                            extra_contractor=extra_contractor,
                            status=wo_status,
                            created_by=user
                        )
                    )

            for domain, work_orders in work_orders_to_create.items():
                if work_orders:
                    work_order_model_class = {
                        'SA': SAWorkOrdersModel,
                        'ROAD': RoadWorkOrdersModel,
                        'POWER': PowerWorkOrdersModel,
                        'TI': TIWorkOrdersModel,
                        'CW': CWWorkOrdersModel,
                    }[domain]

                    work_order_model_class.objects.bulk_create(work_orders)

            project_sites.update(status='Done')

            return response.Response({"detail": "Workorders created successfully"},
                                     status=status.HTTP_201_CREATED)
        else:
            return response.Response({"detail": "No project sites found for the given title"},
                                     status=status.HTTP_404_NOT_FOUND)


There is a model called ProjectSiteModel that contains site id and for a bunch of site id there is a title, now based on the business needs It should be possible that workorders partially created, it means for creating the workorders first we have to check that certain site id has not created yet then create the proper work order, while it is saved it needs to be checked that if all workorders for all site ids in a project created the status of projectsitemodel instances should be Done, other wise if there are some site id that has not yet created the workorders that status should be partially Done