[Answered ]-Getting ID instead of Name in list

1👍

You need to specify in the serializer that you want the foreign keys to be displayed as strings:

from rest_framework import serializers

class POLLSerializer(serializers.ModelSerializer):
    categories = serializers.StringRelatedField(many=False)
    sub_categories = serializers.StringRelatedField(many=False)
    color = serializers.StringRelatedField(many=False)
    size = serializers.StringRelatedField(many=False)
    class Meta:
        model = Products
        fields = "__all__"

This displays whatever is returned by the __str__ method of the object, which you can override to return its name.

class Categories(models.Model):
    category_name = models.CharField(max_length=20)
    category_description = models.CharField(max_length=20)
    isactive = models.BooleanField(default=True)

    def __str_(self):
        return self.category_name

class Colors(models.Model):
    color_name = models.CharField(max_length=10)
    color_description = models.CharField(max_length=10)
    isactive = models.BooleanField(default=True)

    def __str_(self):
        return self.color_name

class Size(models.Model):
    size_name = models.CharField(max_length=10)
    size_description = models.CharField(max_length=20)
    isactive = models.BooleanField(default=True)

    def __str_(self):
        return self.size_name

class SUBCategories(models.Model):
    category_name = models.ForeignKey(Categories, on_delete=models.CASCADE)
    sub_categories_name = models.CharField(max_length=20)
    sub_categories_description = models.CharField(max_length=20)
    isactive = models.BooleanField(default=True)

    def __str_(slef):
        return self.category_name

Alternative

If you do not want to touch your serializer (StringRelatedField is read-only, which may mean there are certain operations you cannot do, like POST), you can always add extra parameters to your serialized data by doing something like this:

def show(request):
    showall = Products.objects.filter(isactive=True) 
    print("show all data:",showall)
    serializer = POLLSerializer(showall,many=True)
    
    data = serializer.data
    
    for i in range(len(data)):
        product = Products.objects.filter(id=data[i]['id']).first()
        data[i]['category_display_name'] = product.categories.__str__()
        data[i]['color_display_name'] = product.color.__str__()
        data[i]['size_display_name'] = product.size.__str__()
        data[i]['sub_category_display_name'] = product.sub_categories.__str__()
   
    return render(request,'polls/product_list.html',{"data":data})

Then, you can simply use the category_display_name, color_display_name, size_display_name and sub_category_display_name to render your table in the template (of course, you need to define the __str__ methods of the Categories, Colors, Size and SUBCategories models here too). This may lead to performance issues when working with large databases, though, so not really recommended…

👤Clerni

Leave a comment