Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

python - How to get Django to reference the name instead of the id/pk

I am currently working on a tutoring API using Python and Django. I want the Tutor Model to show all the tutees (yes, that is a word) and the tutee to show the tutor. Right now, it works correctly but it references the id/pk of the model so I really don't know who or what it refers to.

GET a tutee :

{
"id": 1
"full_name": "First Last",
"email": "email@emailprovidercom",
"tutor": 1,
"subjects": "SubjectX"
}

As you can see, the tutor is a number instead of a name.

GET a tutor:

 {
    "id": 1,
    "full_name": "First Last",
    "email": "[email protected]",
    "notes": "XXXXX XX XX X X  X X X  X XXXXXX XX X X  X X ",
    "tutees": [
        1
    ]
}

The same thing happens here.

I am using Django Rest Framework.

My models

class Tutor(models.Model):
    full_name = models.CharField(max_length=40)
    email = models.EmailField()
    notes = models.TextField()

    def __str__(self):
        return self.full_name


class Tutee(models.Model):
    full_name = models.CharField(max_length=40)
    email = models.EmailField()
    tutor = models.ForeignKey(Tutor, on_delete=models.CASCADE, related_name="tutees")
    subjects = models.CharField(max_length=13)

    def __str__(self):
        return self.full_name

My Serializers

class TutorSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tutor
        fields = ['id', 'full_name', 'email', 'notes', 'tutees']


class TuteeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tutee
        fields = ['id', 'full_name', 'email', "tutor", "subjects"]

My Viewsets

class TutorAPI(viewsets.ModelViewSet):
    queryset = Tutor.objects.all()
    serializer_class = TutorSerializer
    permission_classes = [permissions.IsAuthenticated]
    lookup_field = "full_name"


class TuteeAPI(viewsets.ModelViewSet):
    queryset = Tutee.objects.all()
    serializer_class = TuteeSerializer
    permission_classes = [permissions.IsAuthenticated]
    lookup_field = "full_name"

If you need more, please comment and I will edit my question! Even if you just mark it as duplicate, that would be helpful.

question from:https://stackoverflow.com/questions/65832034/how-to-get-django-to-reference-the-name-instead-of-the-id-pk

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe this would help if You wanna get only string representation

class TuteeSerializer(serializers.ModelSerializer):
    tutor = serializers.StringRelatedField(many=True)
    class Meta:
        model = Tutee
        fields = ['id', 'full_name', 'email', "tutor", "subjects"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...