我一直在尝试使用Django Rest Framework设计一个用于创建移动应用程序的rest API。我可以为商店列表设计一个API,显示商店所有者(商家)信息,商店信息,商店类别和产品,但不显示产品图片。为什么我的代码没有显示产品图片?任何人都可以提供一个想法或建议为什么它不起作用?
我的代码
my 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)
Serializers.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
答案 0 :(得分:1)
在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',)
因此,您将获得实际的图像名称和位置。