Home > database >  'utf-8' codec can't decode byte 0xff in position 0: invalid start byte UnicodeDecodeE
'utf-8' codec can't decode byte 0xff in position 0: invalid start byte UnicodeDecodeE

Time:10-14

i am working with djangorestFramework and in the serializers i have an image which when i fetch it tells me that 'utf-8' error:

it is a model:

from django.db import models
from .categoria import Categoria


class Productos(models.Model):
    
    
    def productos_directory_path(instance,filename):
        return 'productos/{0}'.format(filename)
    
    id_productos = models.AutoField(primary_key=True)
    id_categoria = models.ForeignKey(Categoria, on_delete=models.CASCADE)
    codigo = models.IntegerField(default=0)
    producto = models.CharField('producto', max_length = 100)
    imagen = models.ImageField(upload_to=productos_directory_path, null=True,blank=True)
    stock = models.IntegerField(default=0)
    precio_compra = models.FloatField(default=0.0)
    precio_venta = models.FloatField(default=0.0)
    venta = models.IntegerField(default=0.0)
    fecha = models.DateTimeField(auto_now_add=True)

    
    def __str__(self):
        return self.producto

it is a serializer

from rest_framework import serializers

from backend.models.productos import Productos

from backend.models.categoria import Categoria
from backend.serializers.CategoriaSerializers import Categoriaserializers

class ProductosSerializers(serializers.ModelSerializer):
    #Categorias = Categoriaserializers()
    class Meta:
        model = Productos
        fields = "__all__"

this on localhost:8000/products returns this:

    [
    {
        "id_productos": 8,
        "codigo": 12,
        "producto": "Carro",
        "imagen": "http://localhost:8000/media/productos/bulgakov-mijail-el-maestro-y-margarita.jpg",
        "stock": 0,
        "precio_compra": 0.0,
        "precio_venta": 0.0,
        "venta": 0,
        "fecha": "2021-10-12T16:02:06.167755Z",
        "id_categoria": 2
    }
]

but in addition to that, I need to bring in the product category and I do it this way:

from rest_framework import serializers

from backend.models.productos import Productos

from backend.models.categoria import Categoria
from backend.serializers.CategoriaSerializers import Categoriaserializers

class ProductosSerializers(serializers.ModelSerializer):
    Categorias = Categoriaserializers()
    class Meta:
        model = Productos
        fields = "__all__"
        #fields =  ["id_productos","codigo","producto","imagen","stock","precio_compra","precio_venta","venta","fecha","Categorias"]
        
    def to_representation(self, obj):
        productos = Productos.objects.get(id_productos=obj.id_productos)
        categorias = Categoria.objects.get(productos=obj.id_productos)
            
        return{
            'id_producto': productos.id_productos,
            "codigo": productos.codigo,
            "producto" : productos.producto ,
            "imagen": str(productos.imagen),
            "stock": productos.stock,
            "precio_compra" : productos.precio_compra,
            "precio_venta": productos.precio_venta,
            "venta": productos.venta,
            "fecha": productos.fecha,
            "Categorias":{
                "id_categoria": categorias.id_categoria,
                "categoria": categorias.categoria,
                "fecha": categorias.fecha
            }
        }

and I get this back

[
    {
        "id_producto": 8,
        "codigo": 12,
        "producto": "Carro",
        "imagen": "productos/bulgakov-mijail-el-maestro-y-margarita.jpg",
        "stock": 0,
        "precio_compra": 0.0,
        "precio_venta": 0.0,
        "venta": 0,
        "fecha": "2021-10-12T16:02:06.167755Z",
        "Categorias": {
            "id_categoria": 2,
            "categoria": "Componentes",
            "fecha": "2021-10-11T15:50:24.101968Z"
        }
    }
]

as you can see I get the image as ("products/bulgakov-mijail-the-teacher-and-margarita.jpg ) by the

"imagen": str(products.imagen)

because if I remove it, I get the following error message enter image description here

how can I get the ulr of the image as in the first code snippet, and remove the str()

thanks in advance

CodePudding user response:

you didnt say what type imagen is but if its ImageField there is a property for named url you can wrote "imagen": products.imagen.url instead of "imagen": str(products.imagen)

  • Related