标签:ext nbsp ali 需要 method lock ola tin text
class ChrDetailSerializer(serializers.ModelSerializer): """ 年代地层详情序列化 """ class Meta: model = Address_Lis fields = (‘region‘,) class LisDetailSerializer(serializers.ModelSerializer): """ 岩石地层序列化 """ parent_categry = ChrDetailSerializer() class Meta: model = Address_Lis fields = (‘region‘, ‘parent_categry‘) class PolarizedTypeSeriliazer(serializers.ModelSerializer): """ 偏光类型序列化 """ class Meta: model = PolarizedType fields = (‘pol_type‘,) class LithologicalSeriliazer(serializers.ModelSerializer): """ 岩性序列化 """ class Meta: model = Lithological fields = (‘lit_des‘,) class RockDetailSerializer(serializers.ModelSerializer): """ 详情序列化 """ pol_type = PolarizedTypeSeriliazer() lit_des = LithologicalSeriliazer() area_detail = LisDetailSerializer() class Meta: model = Rock fields = ‘__all__‘
于是前端就不高兴了,不行这样的格式,前端说写起来有点麻烦,于是乎改呗。
好像是解决了,一看我去,完了,这个图片的字段不是完整的url啊,一想,我不可能把域名放在setting中,然后拼接吧,就太low了。
class RockDetailSerializer(serializers.ModelSerializer): """ 详情序列化 """ detail = serializers.SerializerMethodField(read_only=True) area_detail = serializers.SerializerMethodField(read_only=True) pol_type = serializers.SerializerMethodField(read_only=True) lit_des = serializers.SerializerMethodField(read_only=True) well_name = serializers.SerializerMethodField(read_only=True) class Meta: model = Rock fields = (‘detail‘, ‘area_detail‘, ‘pol_type‘, ‘lit_des‘, ‘depth‘, ‘well_name‘) def get_detail(self, obj): try: return {‘id‘: obj.id, ‘image‘: obj.image.url, ‘lit_com‘: obj.lit_com, ‘multiple‘: obj.multiple, ‘color‘: obj.color, ‘pal_fea‘: obj.pal_fea, ‘lit_fea‘: obj.lit_fea, ‘por_fea‘: obj.por_fea} except: return None def get_area_detail(self, obj): try: add_obj = obj.area_detail return str(add_obj.parent_categry.region) + ‘-‘ + str(add_obj.region) except: return None def get_pol_type(self, obj): try: return obj.pol_type.pol_type except: return None def get_lit_des(self, obj): try: return obj.lit_des.lit_des except: return None def get_well_name(self, obj): try: return obj.area_detail.parent_categry.parent_categry.region except: return None
class RockBasicSerializer(serializers.ModelSerializer): class Meta: model = Rock fields = (‘id‘, ‘image‘, ‘lit_com‘, ‘multiple‘, ‘color‘, ‘pal_fea‘, ‘lit_fea‘, ‘por_fea‘) class RockDetailSerializer(serializers.ModelSerializer): """ 详情序列化 """ detail = serializers.SerializerMethodField(read_only=True) area_detail = serializers.SerializerMethodField(read_only=True) pol_type = serializers.SerializerMethodField(read_only=True) lit_des = serializers.SerializerMethodField(read_only=True) well_name = serializers.SerializerMethodField(read_only=True) class Meta: model = Rock fields = (‘detail‘, ‘area_detail‘, ‘pol_type‘, ‘lit_des‘, ‘depth‘, ‘well_name‘) def get_detail(self, obj): try: rock_obj = RockBasicSerializer(obj, many=False, context={‘request‘: self.context[‘request‘]}).data return rock_obj except: return None def get_area_detail(self, obj): try: add_obj = obj.area_detail return str(add_obj.parent_categry.region) + ‘-‘ + str(add_obj.region) except: return None def get_pol_type(self, obj): try: return obj.pol_type.pol_type except: return None def get_lit_des(self, obj): try: return obj.lit_des.lit_des except: return None def get_well_name(self, obj): try: return obj.area_detail.parent_categry.parent_categry.region except: return None
哎哎哎,较好的解决了,夸自己一下。
标签:ext nbsp ali 需要 method lock ola tin text
原文地址:https://www.cnblogs.com/lyq-biu/p/10334247.html