daily pastebin goal
27%
SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class CandidateProfile(models.Model):
  2. user = models.OneToOneField(
  3.     User, on_delete=models.CASCADE, related_name="user")
  4. exp = models.IntegerField(null=True, blank=True)
  5.    
  6. class CandidateProfileSerializer(serializers.ModelSerializer):
  7.     id = serializers.IntegerField(source='pk', read_only=True)
  8.     username = serializers.CharField(source='user.username')
  9.     email = serializers.CharField(source='user.email')
  10.     groups = serializers.RelatedField(read_only=True)
  11.     password = serializers.CharField(max_length=128, source='user.password,read_only=True')
  12.  
  13.     class Meta:
  14.         model = CandidateProfile
  15.         fields = ('id', 'username', 'password', 'email', 'groups')
  16.         depth = 1
  17.  
  18.     def update(self, instance, validated_data):
  19.         print("In Update" + '*' * 50)
  20.         user = User.objects.get(pk=instance.user.pk)
  21.         user = instance.user
  22.         user.email = validated_data.get('user.email', user.email)
  23.         user.first_name = validated_data.get('user.first_name',
  24.                                          user.first_name)
  25.         user.last_name = validated_data.get('user.last_name', user.last_name)
  26.         user.save()
  27.  
  28.         instance.gender = validated_data.get('gender', instance.gender)
  29.         instance.save()
  30.  
  31.         return instance
  32.  
  33.     def create(self, validated_data):
  34.         print('*' * 100)
  35.         print(validated_data)
  36.         user_data = validated_data.pop('user')
  37.         print(user_data)
  38.         user = User.objects.create_user(**user_data)
  39.         g = Group.objects.get(name="Candidate")
  40.         g.user_set.add(user)
  41.         user.save()
  42.         print(validated_data)
  43.         print('*' * 100)
  44.         profile = CandidateProfile.objects.create(user=user, **validated_data)
  45.         return user
  46.    
  47. class CandidateRegister(APIView):
  48.     def get(self, request, format=None):
  49.         candidate_list = User.objects.filter(groups=Group.objects.get(
  50.             name="Candidate"))
  51.         serializer = CandidateProfileSerializer(candidate_list, many=True)
  52.         return Response(serializer.data)
  53.  
  54.     def post(self, request, format=None):
  55.         serializer = CandidateProfileSerializer(data=request.data)
  56.         if serializer.is_valid():
  57.             serializer.save()
  58.             return Response(serializer.data, status=status.HTTP_201_CREATED)
  59.         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  60.    
  61. Got AttributeError when attempting to get a value for field `username` on serializer `CandidateProfileSerializer`.
  62. The serializer field might be named incorrectly and not match any attribute or key on the `User` instance.
  63. Original exception text was: 'CandidateProfile' object has no attribute 'username'.
  64.    
  65. class CandidateProfileSerializer(serializers.ModelSerializer):
  66.     id = serializers.IntegerField(source='pk', read_only=True)
  67.     username = serializers.SerializerMethodField()
  68.  
  69.     def get_username(self, obj):
  70.         return obj.user.username
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top