I want to update a record, but the field is determined by string. Is it possible to do so?
This is my model:
class Wallet(models.Model):
user_id = models.IntegerField(blank=True, null=True)
wallet1 = models.DecimalField(max_digits=15, decimal_places=2, blank=True, null=True)
wallet2 = models.DecimalField(max_digits=15, decimal_places=2, blank=True, null=True)
This is my code:
amount = 200
transfer_from = 'wallet1'
transfer_to = 'wallet2'
obj = Wallet.objects.get(user_id=1)
obj.transfer_from = obj.transfer_from - amount
obj.transfer_to = obj.transfer_to + amount
obj.save()
Django only recognize the field when i write this:
obj.wallet1 = obj.wallet1 - amount
but it doesn't recognize this:
transfer_from = 'wallet1'
obj.transfer_from = obj.transfer_from - amount
Is said 'Wallet' object has no attribute 'transfer_from'. Thank you.