#店主表
class Shop(models.Model):
name = models.CharField(max_length=50)
passwd = models.CharField(max_length=255)
class Meta:
db_table = 'shop'
#商品表
class Goods(models.Model):
name = models.CharField(max_length=50)
price = models.DecimalField(max_digits=7,decimal_places=2)
image_url = models.CharField(max_length=255)
shop = models.ForeignKey(Shop,on_delete=models.CASCADE,related_name='goods')
class Meta:
db_table = 'goods'
#如果要查看店主表下的商品直接方法就是序列化,格式添加
#序列化商品表
class GoodsModelSerializer(serializers.ModelSerializer):
class Meta:
model = Goods
fields = '__all__'
#序列化店主表
class ShopModelSerializer(serializers.ModelSerializer):
#goods的变量名必须跟商品表里的related_name='goods',goods一样,直接书写商品序列化many=True,就会生成json字符串,然后直接书写业务逻辑即可
goods = GoodsModelSerializer(many=True)
class Meta:
model = Shop
fields = '__all__'