是否可以合并两个字段,例如,age_from
和age_to
到名为age
的键和字典值。就像这样:
age : {
from: 18,
to: null
}
我的模特:
# models.py
class Banner(models.Model):
text = models.CharField(max_length=255)
description = models.TextField()
image = WEBPField(
verbose_name='Image',
upload_to=banner_folder,
)
link = models.URLField()
view_count = models.PositiveIntegerField(default=0)
age_from = models.PositiveSmallIntegerField()
age_to = models.PositiveSmallIntegerField()
def __str__(self):
return f"{self.id}. {self.text}"
def count_increase(self):
self.view_count += 1
self.save()
我的序列化器:
# serializers.py
class BannerSerializer(ModelSerializer):
image = Base64ImageField()
class Meta:
model = Banner
fields = ["id", "text", "description", "image", "link"]
1条答案
按热度按时间kiayqfof1#
是的,您可以创建自定义字段:
AgoField
仍然需要一些额外的微调,如验证器等。但这是“快乐之路”的基本实现。