gh_UserManager/apps/api/apps/profile/serializers.py
bermooda-company 54d5891edf user
2026-08-23 23:59:14 +03:30

50 lines
1.4 KiB
Python

from rest_framework import serializers
from apps.profile.models import Profile
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = (
"id",
"user",
"profile_type",
"is_primary",
"is_verified",
"verified_at",
"headline",
"bio",
"location",
"skills",
"experience_years",
"company_name",
"registration_number",
"website_url",
"organization",
"data",
"created_at",
"updated_at",
)
read_only_fields = (
"id",
"is_verified",
"verified_at",
"created_at",
"updated_at",
)
def validate(self, attrs):
if self.instance is None:
user = attrs.get("user")
profile_type = attrs.get("profile_type")
else:
user = self.instance.user
profile_type = attrs.get("profile_type", self.instance.profile_type)
if Profile.objects.filter(user=user, profile_type=profile_type).exists():
if self.instance is None or self.instance.profile_type != profile_type:
raise serializers.ValidationError(
"A profile of this type already exists for this user."
)
return attrs