Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

DRF: How can I show all fields of a m2m field instead of just the name

I’m building a simple Lead Generator using Django Rest Framework.
I’m trying to show a list of "assigned facilities" inside a lead using django’s many to many fields. But all it will show inside the API is the id of each of the facilities associated to the many to many field. How do I access more than just the name using DRF? I basically need to show the name, a description and a picture of the facility from each facility record.

serializers.py

class LeadUpdateSerializer(serializers.ModelSerializer):
    is_owner = serializers.SerializerMethodField()
    class Meta:
        model = Lead
        fields = (
            "id",
            "first_name",
            "last_name",
            "PrimaryAddress",
            "assigned_facilities",
        )
        read_only_fields = ("id", "is_owner")

    def get_is_owner(self, obj):
        user = self.context["request"].user
        return obj.agent == user

models.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Facility(models.Model):
    UUID = models.CharField(max_length=150, null=True, blank=True)
    Name = models.CharField(max_length=150, null=True, blank=False)
    mainimage = models.ImageField(null=True, blank=True)
    FacilityDescription = models.TextField(max_length=1000, null=True, blank=True)

    def __str__(self):
        return self.Name

class Lead(models.Model):
    assigned_facilities = models.ManyToManyField(Facility,  related_name='assigned_facilities')
    created_at = models.DateTimeField(auto_now_add=True)
    first_name = models.CharField(max_length=40, null=True, blank=True)
    last_name = models.CharField(max_length=40, null=True, blank=True)


    def __str__(self):
        return f"{self.first_name} {self.last_name}"

>Solution :

We can like below:

class FacilitySerializer(serializers.ModelSerializer)
    class Meta:
        fields = (
            "id",
            "Name",
            "mainimage",
            "FacilityDescription",
        )


class LeadUpdateSerializer(serializers.ModelSerializer):
    assigned_facilities = FacilitySerializer(many=True)
    is_owner = serializers.SerializerMethodField()

    class Meta:
        model = Lead
        fields = (
            "id",
            "first_name",
            "last_name",
            "PrimaryAddress",
            "assigned_facilities",
        )
        read_only_fields = ("id", "is_owner")

    def get_is_owner(self, obj):
        user = self.context["request"].user
        return obj.agent == user
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading