gpt4 book ai didi

How to captalize letter on django rest serialize(如何在Django REST序列化上大写字母)

转载 作者:bug小助手 更新时间:2023-10-24 17:55:10 25 4
gpt4 key购买 nike



In my database i have a nome field, and in this charfield all data is uppercase. I need return in the title format, just title() method does.
example:

在我的数据库中,我有一个Nome字段,在这个Charfield中,所有数据都是大写的。我需要以标题格式返回,只有title()方法需要。示例:



data in my field name: CAFE SANTA CLARA SOLUVEL GRAN. RF 50G-12501

我域名中的数据:Cafe Santa Clara Soluvel Gran。射频50G-12501



i need return like that: Cafe Santa Clara Soluve Gran Rf 50G-12501

我需要这样的回报:圣克拉拉咖啡馆Soluve Gran RF 50g-12501



I use django rest serializer to return this data.

我使用Django REST序列化程序返回此数据。



Whats the best way to do this? In my model, serializer class or my view?

做这件事最好的方法是什么?在我的模型、序列化程序类还是我的视图中?



Tks!

谢谢



model:

型号:



class Produto(models.Model):
def __str__(self):
return self.nome.encode('utf-8')

def __unicode__(self):
return self.nome.encode('utf-8')

categoria = models.ForeignKey(ProdutoCategoria, null=True, blank=True)
marca = models.ForeignKey(ProdutoMarca, null=True)
nome = models.CharField(max_length=256, null=True) #its this column
unidade = models.CharField(max_length=256, null=True, db_column='unid')
ean = models.CharField(max_length=256, null=True)
ncm = models.DecimalField(max_digits=65, null=True, decimal_places=2)
fator = models.DecimalField(max_digits=100, null=True, decimal_places=2)
fornecedor = models.CharField(max_length=256, null=True)
imagem = models.TextField(blank=True, null=True)
id_externo = models.IntegerField(null=True, blank=True)
top = models.NullBooleanField(null=True, blank=True)


serializer:

串行器:



class ProdutoSerializer(serializers.ModelSerializer):
marca = serializers.CharField(source='marca.nome')
categoria_pai = serializers.IntegerField(source='categoria.pai.id')
class Meta:
model = Produto
fields = (
'ean',
'id',
'nome',
'imagem',
'marca',
'categoria',
'categoria_pai'
)

更多回答

Why to return it in this format? I will suggest to let the transformation of field to be done at the client side. If you do not want that, in that case you may create serializer methods and map those methods with the fields in case it is something which is to be replicated in many views. If it is for single view. Better transform the serialized result in the view

为什么要以这种格式退货?我会建议让领域的转变在客户端完成。如果您不想这样做,在这种情况下,您可以创建序列化程序方法,并将这些方法与字段映射,以防它是要在许多视图中复制的东西。如果是单视的话。更好地转换视图中的序列化结果

优秀答案推荐

The best way is use your serializer:

最好的方法是使用您的序列化程序:



Something like this:

大概是这样的:



class ServiceSerializer(serializers.ModelSerializer):
category = ServiceCategorySerializer()

service = serializers.SerializerMethodField()

def get_service(self, obj):
return obj.service.upper() if obj.service else None


This is the first way :)
The second is to change tour to_representation, like this:

这是第一种方法:)第二种方法是将TUR更改为_REPORTATION,如下所示:



class ServiceSerializer(serializers.ModelSerializer):
category = ServiceCategorySerializer()

class Meta:
model = Service
fields = ('service', 'category')

def to_representation(self, instance):
data = super(ServiceSerializer, self).to_representation(instance=instance)
data['service'] = data['service'].lower() if data['service'] else data['services']
return data


The problem is that :) before each save - you should reverse the process - especially in the second case. But in the first scenario - you will also get from the front the changed value, so basically after some finite time you will have all DB rows rewritten ;)

问题是:)在每次保存之前,您应该颠倒这个过程--特别是在第二种情况下。但在第一个场景中-您还将从前面获得更改的值,因此基本上在一段有限时间后,您将重写所有数据库行;)



As someone suggested - why you do not want to do this on Front side?

正如有人建议的那样--为什么您不想在正面执行此操作?



## First Letter Capital.
def get_name(self, obj):
return obj.name.capitalize()

更多回答

Please add an explanation to your code.

请在您的代码中添加说明。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com