diff --git a/vacancy/managers.py b/vacancy/managers.py index ddacd6a7..62e24016 100644 --- a/vacancy/managers.py +++ b/vacancy/managers.py @@ -37,14 +37,11 @@ def get_vacancy_for_detail_view(self): class VacancyResponseManager(Manager): def get_vacancy_response_for_list_view(self): - return ( - self.get_queryset() - .select_related( - "vacancy", - "vacancy__project", - "vacancy__project__leader", - "accompanying_file", - ) + return self.get_queryset().select_related( + "vacancy", + "vacancy__project", + "vacancy__project__leader", + "accompanying_file", ) def get_vacancy_response_for_email(self): @@ -75,6 +72,7 @@ def get_vacancy_response_for_detail_view(self): .only( "user", "vacancy", + "vacancy__role", "why_me", "accompanying_file", "is_approved", diff --git a/vacancy/serializers.py b/vacancy/serializers.py index ba324a65..fa1d1df8 100644 --- a/vacancy/serializers.py +++ b/vacancy/serializers.py @@ -25,6 +25,7 @@ class RequiredSkillsWriteSerializerMixin(RequiredSkillsSerializerMixin): class AbstractVacancyReadOnlyFields(serializers.Serializer): """Abstract read-only fields for Vacancy.""" + datetime_closed = serializers.DateTimeField(read_only=True) @@ -149,7 +150,7 @@ class Meta: ] -class VacancyResponseListSerializer(serializers.ModelSerializer[VacancyResponse]): +class VacancyResponseListSerializer(serializers.ModelSerializer): is_approved = serializers.BooleanField(read_only=True) user = UserDetailSerializer(read_only=True) user_id = serializers.IntegerField(write_only=True) @@ -159,6 +160,9 @@ class VacancyResponseListSerializer(serializers.ModelSerializer[VacancyResponse] required=False, allow_null=True, ) + vacancy_role = ( + serializers.SerializerMethodField() + ) # SerializerMethodField to access related field class Meta: model = VacancyResponse @@ -170,11 +174,15 @@ class Meta: "accompanying_file", "is_approved", "vacancy", + "vacancy_role", ] extra_kwargs = { "user_id": {"write_only": True}, } + def get_vacancy_role(self, obj: VacancyResponse) -> str: + return obj.vacancy.role + def validate(self, attrs): vacancy = attrs["vacancy"] user = self.validate_user_exists(attrs["user_id"]) @@ -199,6 +207,7 @@ def create(self, validated_data): class VacancyResponseFullFileInfoListSerializer(VacancyResponseListSerializer): """Returns full file info.""" + accompanying_file = UserFileSerializer(read_only=True)