0
votes

I am having some issues with my DRF Serializers. I essentially have a model that has Django users as Foreign keys so I can see who is attached to a job.

When I try and resolve these user ID's nested inside my Job serializer using a User serializer I only see the ID, but when I use the User serializer on it's own not nested I get the correct fields returned. Below is my code snippets. Any help would be great.

models.py

from profiles.models import UserProfile
class Job(models.Model):

    name = models.CharField(max_length=256, blank=False)
    designer_one = models.ForeignKey(UserProfile, related_name='designer_one', on_delete=models.DO_NOTHING)
    designer_two = models.ForeignKey(UserProfile, related_name='designer_two', on_delete=models.DO_NOTHING)

    def __str__(self):
        return self.name

    class Meta(object):
        verbose_name = "Job"
        verbose_name_plural = "Jobs"
        ordering = ['name']

serializers.py

from django.contrib.auth.models import User
class UsersSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'email', 'first_name', 'last_name')

class JobsSerializer(serializers.ModelSerializer):
    tasks = TasksSerializer(many=True, read_only=True)
    designer_one = UsersSerializer(many=False, read_only=True)
    designer_two = UsersSerializer(many=False, read_only=True)

    class Meta:
        model = Job
        fields = ('id', 'name', 'designer_one', 'designer_two', 'tasks')

What I get returned from UsersSerializer API View

[
{
    "id": 1,
    "email": "[email protected]",
    "first_name": "Admin",
    "last_name": "User"
},
{
    "id": 2,
    "email": "[email protected]",
    "first_name": "",
    "last_name": ""
}
]

What I get returned from JobsSerializer API View

{
    "id": 1,
    "name": "Test job",
    "designer_one": {
        "id": 1
    },
    "designer_two": {
        "id": 1
    },
    "tasks": [
        {
            "id": 1,
            "name": "Test Task",
            "job": 1
        }
    ]
}
1
Remove designer_one = UsersSerializer(many=False, read_only=True) designer_two = UsersSerializer(many=False, read_only=True) from JobsSerializer. It should show, if not add depth=1 for JobsSerializer - Anup Yadav
Thanks for the advice, I removed those lines but all I am returned then is the ID as an int. I then added depth = 1 both with and without those lines and nothing changed. - samroberts707
Have then added depth=1 in JobsSerializer. in class Meta: - Anup Yadav
@AnupYadav That worked, thanks a lot. I did need to make it depth=2 to get the correct level though. Do you know if there is a way to limit what fields are returned? Ideally I don't want passwords and other information like that sent via the api - samroberts707
Great, I could help you, enjoy :) - Anup Yadav

1 Answers

0
votes

The issue is that you are using UsersSerializer for model User, meanwhile designer_one and designer_two are of type UserProfiile.

You can allow DRF generate the nested serializers for you using the depth option as @Anup Yadav suggested but if you want to have control over which fields are displayed, you need to create your own serializer for Userprofile and use it in the Job serializer