python - 无法在 API 中显示产品图像

标签 python django api django-rest-framework

我一直在尝试使用 Django Rest Framework 设计一个 Rest API 来创建移动应用程序。我可以为商店列表设计一个 API,其中显示商店所有者(商家)信息、商店信息、商店类别和产品,但不显示产品图像。为什么我的代码没有显示产品图片?谁能给我提供一个想法或建议为什么它不起作用?

我的代码

我的 models.py

class Store(models.Model):
    merchant = models.ForeignKey(Merchant)
    name_of_store = models.CharField(max_length=100)
    store_off_day = MultiSelectField(choices=DAY, max_length=7, default='Sat')
    store_categories = models.ManyToManyField('StoreCategory',blank=True)

    class Meta:
        verbose_name = 'Store'


class Product(models.Model):
    store = models.ForeignKey(Store)
    name_of_product = models.CharField(max_length=120)
    description = models.TextField(blank=True, null=True)
    price = models.DecimalField(decimal_places=2, max_digits=20)
    # categories = models.ManyToManyField('Category',blank=True)


class ProductImage(models.Model):
    product = models.ForeignKey(Product)
    image = models.ImageField(upload_to='products/images/')
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

class StoreCategory(models.Model):
    product = models.ForeignKey(Product,null=True, on_delete=models.CASCADE,related_name="store_category")
    store_category = models.CharField(choices=STORE_CATEGORIES, default='GROCERY', max_length=10)

序列化器.py

class ProductImageSerializer(ModelSerializer):
    class Meta:
        model = ProductImage
        fields  =   ('id','image', )

class ProductSerializers(ModelSerializer):
    image = ProductImageSerializer(many=True,read_only=True)
    class Meta:
        model = Product
        fields=('id','image','name_of_product','description','price','active',)

class StoreCategorySerializer(ModelSerializer):
    product = ProductSerializers(read_only=True)
    class Meta:
        model = StoreCategory

class StoreSerializer(ModelSerializer):
    # url = HyperlinkedIdentityField(view_name='stores_detail_api')
    store_categories = StoreCategorySerializer(many=True) 
    merchant = MerchantSerializer(read_only=True)
    class Meta:
        model = Store
        fields=("id",
                # "url",
                "merchant",
                "store_categories",
                "name_of_store",
                "store_contact_number",
                "store_off_day",
                )

我的 API

enter image description here

最佳答案

在您的 models.py 中创建:

import os

从 ProductImage 模型中删除 Product 外键:

class ProductImage(models.Model):
    image = models.ImageField(upload_to='products/images/')
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    @property
    def imagename(self):
        return str(os.path.basename(self.image.name))

将图像外键添加到您的产品中

class Product(models.Model):
    image = models.ForeignKey(ProductImage,blank=True,null=True)
    store = models.ForeignKey(Store)
    name_of_product = models.CharField(max_length=120)
    description = models.TextField(blank=True, null=True)
    price = models.DecimalField(decimal_places=2, max_digits=20)
    # categories = models.ManyToManyField('Category',blank=True)

然后在你的serializers.py中

class ProductImageSerializer(ModelSerializer):
    class Meta:
        model = ProductImage
        fields  =   ('id','imagename', )

class ProductSerializers(ModelSerializer):
    image = ProductImageSerializer(many=False,read_only=True) #only one image used
    class Meta:
        model = Product
        fields=('id','image','name_of_product','description','price','active',)

这样您就可以获得实际的图像名称和位置。

关于python - 无法在 API 中显示产品图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38549312/

相关文章:

python - 如何使用带有数据的数据库在 Django 中运行测试?

python - Django 上下文列表键

python - Django 日志记录功能?

javascript - Watson Conversation - Oracle 数据库集成

ruby - 我想创建某种 API 以通过唯一 key 运行 ruby​​ 程序

python - 如何在 python 中使用正则表达式将 HTML 子字符串附加到匹配的字符串中?

python - 使用 numpy >= 1.13 将特定字段视为 ndarray

python - 在 Dash 中下载 csv 文件

Python igraph 顶点索引

java - 去除特殊字符java