Home > Back-end >  How to retrieve foreign field name in Django Rest Framework
How to retrieve foreign field name in Django Rest Framework

Time:09-24

I have 2 models one for a user and another for associated files for the user

Models.py

class IndividualUser(models.Model):
    membership_id = models.CharField(primary_key=True, max_length=100, default=1)
    profile_image = models.ImageField(blank=True, upload_to ="individual_member_profile/", null=True)
    firstname = models.CharField(max_length=100)
    lastname = models.CharField(max_length=100)

class MemberCatalogue(models.Model):
    membership_id = models.ForeignKey(IndividualUser, default=None,on_delete=models.CASCADE, related_name="member_catalogue")
    files = models.FileField(upload_to="individualmembercatalogue/")

Currently Iam getting absolute path for files. I need a filename uploaded by user for which Iam trying to get "files" field and and then split it with "/". But Iam struggling to get the files field from MemberCatalogue.

My serializers look like this at the moment:

class MemberCatalogueSerializer(serializers.ModelSerializer):
    files = serializers.FileField()

    class Meta:
        model = MemberCatalogue
        fields = ['id','membership_id', 'files']

class IndividualMembersSerializer(serializers.ModelSerializer):
    member_catalogue = MemberCatalogueSerializer(many=True)
    filename = serializers.SerializerMethodField('get_filename')

    def get_filename(self, obj):
        return obj.individualmember.files

    class Meta:
        model = IndividualMembers
        fields = "__all__"

But I cant get the desired output. Expected output is like

    {
        "membership_id": "142369ca1b1484d8d9d6d87fdc8543db",
        "member_catalogue": [
            {
                "id": 156,
                "membership_id": "142369ca1b1484d8d9d6d87fdc8543db",
                "files": "http://127.0.0.1:8000/individualmembercatalogue/some_file.pdf"
                "filename" : "some_file.pdf"
            }
        ],
        "profile_image": null,
        "firstname": "John",
        "lastname": "Doe",
    }

CodePudding user response:

You are using the wrong model in your IndividualMembersSerializer. I think you are trying to edit the way filename is displayed in the wrong serializer, since files is a field on MemberCatalogue and not on IndividualUser.

Here is how I would do it:

class MemberCatalogueSerializer(serializers.ModelSerializer):
    filename = serializers.SerializerMethodField()

    def get_filename(self, obj):
        return str(obj.files).split('/')[1]
    class Meta:
        model = MemberCatalogue
        fields = ['id','membership_id', 'files', 'filename']

class IndividualMembersSerializer(serializers.ModelSerializer):
    member_catalogue = MemberCatalogueSerializer(many=True)

    class Meta:
        model = IndividualUser
        fields = "__all__"

CodePudding user response:

class MemberCatalogueSerializer(serializers.ModelSerializer):
    filename = serializers.SerializerMethodField()
    class Meta:
        model = MemberCatalogue
        fields = ['membership_id', 'files','filename']

    def get_filename(self, obj):
        return obj.files.url.split('/')[-1]
  • Related