前后联调 外键怎么实现汉字的效果
在django设置自定义序列化
后端代码:
class Carsers(serializers.ModelSerializer):
# 自定义序列化
catename=serializers.SerializerMethodField()
# 获取自定义字段得数据(想让自定义字段显示的内容)
# obj形参
def get_catename(self,obj):
# obj.brand是外键名.band是分类表的类名
return obj.brand.bname
class Meta:
model=Cars
fields="__all__"
前端列表可以直接使用自定义的catename为所属分类列名称
前端代码:
<tr v-for="(item,i) in lst" :key="i">
<th>{{item.id}}</th>
<th>{{item.cname}}</th>
<th>{{item.price}}</th>
<th>{{item.count}}</th>
<th>{{item.color}}</th>
<!-- 自定义序列化字段名 -->
<th>{{item.catename}}</th>
<th>
<button @click="show(i)">获取</button>
<!-- <button @click="xiugai(item.id)">修改</button> -->
<button @click="del(item.id)">删除</button>
</th>
</tr>

浙公网安备 33010602011771号