'Django Rest custom serialize field
My models.py is as follows:
class CoinSetting(models.Model):
coin = models.ForeignKey(Coin, on_delete=models.CASCADE)
TYPES = (
(0, "Amount"),
(1, "Share"),
)
type = models.IntegerField(choices=TYPES, default=0)
type_value = models.FloatField()
and I want to serialize it to looks like that:
"coins": [
{
"coin": ....
"setting": {
"Amount": 10.1
}
}
]
User can choose the form of sharing coin, i.e some amount of coin or percentage of some amount. Field "type" describe the form that user can choose, and field "type_value" is for insert this value (amount or percentage) to the chosen type. So:
- for "Amount", the value is some integer (float field),
- for "Share", the value is integer from 0 to 1 (this is percentage of amount, float field in range (0, 1)).
How can I serialize that model to get that display?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
