Use of queryset inside Primarykeyfield of ModelSerializer in Django Rest

I am making a simple create api for a model which has almost all fields as foriegn key to other models. I am writing create logic inside the serializer. It is working fine. However if I define all the fields as pkrelated field in serializer, it asks for queryset attribute. Why it is asking that. And if I put queryset and import all the objects(queryset= Example.objects.all()) wouldnt it make the code and api slow since it is importing all the objects from Example model. Also, what its use to be honest?

My model:

class Example(TimeStampAbstractModel):
    abc = models.ForeignKey(User,....                                   
    xyz_info = models.ForeignKey(XYZ,...                                
    mnp = models.ForeignKey(MNP,......                                   
    status = models.CharField(....blank=True)

My serializer:

class AttendanceSerializer(serializers.ModelSerializer):
    xyz_info = serializers.PrimaryKeyRelatedField(required=True,
                                                      queryset=XYZ.objects.all())
    mnp= serializers.PrimaryKeyRelatedField(required=True,
                                                 queryset=MNP.objects.all())

    #mnp = serializers.IntegerField(required=True)
    status = serializers.CharField(required=True)


    class Meta:
        model = Attendance
        fields = ['xyz_info','mnp','status']

    def create(self, validated_data):
    .............

In above serializer class, if I hadnt defined all those fileds in the serializer, it works fine. But if I define them as pkfield as above ( I am passing only ids in the body from frontend), it asks for queryset. Why it is asking queryset and what its use there? Also can I defined them as integarfield? When to define as pkfield and when to integarfield??

Back to Top