https://teamsparta.notion.site/0624-0db1692a1d654447a0723e6dac426fa8
# post/serializers.py
class BusinessAreaSerializer(serializers.ModelSerializer):
class Meta:
model = BusinessArea
fields = ['area']
class CompanySerializer(serializers.ModelSerializer):
business_area = BusinessAreaSerializer(many=True)
class Meta:
model = Company
fields = ['company_name', 'business_area']
# post/serializers.py
class JobTypeSerializer(serializers.ModelSerializer):
class Meta:
model = JobType
fields = ['job_type']
class JobPostSerializer(serializers.ModelSerializer):
job_type = JobTypeSerializer()
company = CompanySerializer()
class Meta:
model = JobPost
fields = ['id', 'job_type', 'company', 'job_description', 'salary']
# post/serializers.py
class SkillSetSerializer(serializers.ModelSerializer):
job_posts = JobPostSerializer(many=True)
class Meta:
model = SkillSet
fields = ['name' ,'job_posts']
class JobPostSkillSetSerializer(serializers.ModelSerializer):
job_posts = JobPostSerializer()
skill_set = SkillSetSerializer()
class Meta:
model = SkillSet
fields = ['skill_set' ,'job_posts']
request 1 또는 2 타입으로 둘중 하나로 요청받아서 구현해볼것! (job_type 이 다름)
# post/views.py
class JobView(APIView):
def post(self, request):
job_type = int( request.data.get("job_type", None) )
company_name = request.data.get("company_name", None)
try:
job_type = JobType.objects.get(id=job_type)
except:
return Response({'message': '해당 job_type 이 없습니다!'}, status=status.HTTP_400_BAD_REQUEST)
try:
company = Company.objects.get(company_name=company_name)
except:
company = Company.objects.create(company_name=company_name)
company.save()
request.data['company'] = CompanySerializer(company).data
request.data['job_type'] = JobTypeSerializer(job_type).data
job_serializer = JobPostSerializer(data=request.data)
if job_serializer.is_valid():
job_serializer.save()
return Response({'message': '저장 완료!'}, status=status.HTTP_200_OK)
return Response(job_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# post/views.py
class SkillView(APIView):
permission_classes = [permissions.AllowAny]
def get(self, request):
skills = self.request.query_params.getlist('skills', '')
query = Q()
for skill in skills:
skill_set = SkillSet.objects.get(name=skill)
query |= Q(skill_set=skill_set)
job_post_skill_sets = JobPostSkillSet.objects.filter(query)
job_post_ids = job_post_skill_sets.distinct().values_list('job_post_id')
# job_post_ids = [job_post_skill_set.job_post_id for job_post_skill_set in job_post_skill_sets]
# job_post_ids = list(set(job_post_ids))
job_posts = [JobPost.objects.get(id=id[0]) for id in job_post_ids]
return Response(JobPostSerializer(job_posts, many=True).data, status=status.HTTP_200_OK)