Home > Software design >  Size not being displayed on productdetails page
Size not being displayed on productdetails page

Time:07-18

I am doing CRUD using serializers and foreign keys and I have made a product details page which shows the details of the product that I have clicked. The problem is that the Size(SM,M,L,XL,XXL) itself isn't coming but the id is coming as shown below as shown in the image, the id or number is getting displayed instead of the size itself below is the 'productdetails' function

<tr>
    <td>{{data.id}}</td>
    <td>{{data.title}}</td>
    <td>{{data.price}}</td>
    <td>{{data.sku_number}}</td>
    <td>{{data.product_details}}</td>
    <td>{{data.size}}</td>
    <td>{{data.quantity}}</td>
    <td><img src="{{data.image}}" alt="product image" width="400" height="400"></td>
</tr>

productdetails function

def productdetails(request,id):
    prod = Products.objects.get(id=id)
    product = POLLSerializer(prod)
    return render(request,'polls/productdetails.html',{'data':product.data})

model

class Products(models.Model):
    categories = models.ForeignKey(Categories,on_delete=models.CASCADE)
    sub_categories = models.ForeignKey(SUBCategories,on_delete=models.CASCADE)
    color = models.ForeignKey(Colors,on_delete=models.CASCADE)
    size = models.ForeignKey(Size,on_delete=models.CASCADE)
    image = models.ImageField(upload_to = 'media/',width_field=None,height_field=None,null=True)
    title = models.CharField(max_length=70)
    price = models.CharField(max_length=10)
    sku_number = models.CharField(max_length=10)
    product_details = models.CharField(max_length=1000)
    quantity = models.IntegerField(default=0)
    isactive = models.BooleanField(default=True)

serializer

class POLLSerializer(serializers.ModelSerializer):
    class Meta:
        model = Products
        fields = "__all__"

size model and serializer

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 SizeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Size
        fields = "__all__"

Help is greatly appreciated,thanks!

CodePudding user response:

try this in ypur view.py

def productdetails(request,id):
   prod = Products.objects.get(id=id)
   product = POLLSerializer(prod)
   return render(request,'polls/productdetails.html',{'data':prod.data})
  • Related