[Fixed]-Django Rest Framework – Post Foreign Key

31πŸ‘

βœ…

Your ProfileSerializer has user as readonly. So you need to change that. I would suggest doing it like this

class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model=Profile
        fields=('id','user','email','birthday','gender','bio','points')
        read_only_fields = ('created','updated')

    def to_representation(self, instance):
        self.fields['user'] =  UserSerializer(read_only=True)
        return super(ProfileSerializer, self).to_representation(instance)

If you do it this you could provide your user as plain id for POST

{
  "user": 2,
  "email": "xxx@gmail.com",
  "birthday": "1991-05-28",
  "bio": "qudur",
  "points": 31
}

And when you will read data it will look like this

{
  "user": {
    "id": 2,
    "name": "Name",
    "surname": "Surname",
    ...
  },
  "email": "xxx@gmail.com",
  "birthday": "1991-05-28",
  "bio": "qudur",
  "points": 31
}

4πŸ‘

I’ve noticed Super() throws an error the way it’s mentioned above in the awnser:

return super(ProfileSerializer,self).to_representation(instance)

Error: Type error, object must be an instance or subtype of type

Try the Following:

Models.py

class Program(models.Model):
    name = models.CharField(max_length=225)
    cost = models.IntegerField(default=0)
    description = models.TextField(default="", max_length=555)

class UserProgram(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    program = models.ForeignKey(Program, on_delete=models.CASCADE, related_name="program")

Serializers.py

class ProgramSerializers(serializers.ModelSerializer):
    class Meta:
        model = Program
        fields = "__all__"

class UserProgramSerializers(serializers.ModelSerializer):
    class Meta:
        model = UserProgram
        fields = "__all__"

    #IMPORTANT PART
    def to_representation(self, instance):
        response = super().to_representation(instance)
        response['program'] = ProgramSerializers(instance.program).data
        return response

Views.py

class UserProgramViewset(viewsets.ModelViewSet):
     permission_classes = [
        permissions.IsAuthenticated
     ]
     serializer_class = UserProgramSerializers
     
     def get_queryset(self):
        return UserProgram.objects.filter(user=self.request.user)    

     def perform_create(self, serializer):
        serializer.save(user=self.request.user)

When you call the GET request the following should be the output:
GET Request Output

When you call the POST request you only need to pass the programID and not the whole JSON dictionary!

Hope this helped.

πŸ‘€PdotNJ

Leave a comment