Reputation: 630
I have been trying to design a rest API using Django Rest Framework for creating mobile application. I could design an API for Store list which shows store owner(merchant) information, store information, category of store and Product but product image is not displayed. Why my code is not showing product image? Could anyone please provide me an idea or advice why it is not working?
My code
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",
)
My API
Upvotes: 1
Views: 79
Reputation: 3506
In your models.py create:
import os
Remove Product foreign key from your ProductImage model:
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))
Add image foreign key to your Product instead
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)
and then in your 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',)
So this way you'll get the actual image name and location.
Upvotes: 1