class WithdrawRequests(models.Model):
withdraw_hash = models.CharField(max_length=255, unique=True, db_index=True)
timestamp = models.DateTimeField(auto_now_add=True)
username = models.CharField(max_length=255, unique=False, db_index=True)
currency = models.CharField(max_length=255, unique=False, db_index=True)
user_balance = models.DecimalField(max_digits=300, default=0.0, decimal_places=150)
withdraw_address = models.CharField(max_length=255, unique=False, db_index=True)
withdraw_amount = models.DecimalField(max_digits=30, default=0.0, decimal_places=15)
This is my models.py file.
currency = request.data['currency']
payload = jwt.decode(token, settings.SECRET_KEY)
user = User.objects.get(id=payload['user_id'])
timestamp = datetime.datetime.now()
timestamp = timestamp.timestamp()
withdraw_hash = hashlib.sha256()
withdraw_hash.update(str(timestamp).encode("utf-8"))
withdraw_hash = withdraw_hash.hexdigest()
username = user.username
currency_balance = GAME_CURRENCIES[request.data['currency']]
user_balance = getattr(user, currency_balance)
withdraw_address = request.data['withdraw_address']
withdraw_amount = request.data['withdraw_amount']
if user_balance < withdraw_amount:
return Response({
"message": "Not enough funds."
})
else:
# row format - hash timestamp username currency user_balance withdraw_address withdraw_amount
withdraw = WithdrawRequests()
withdraw.withdraw_hash = withdraw_hash,
withdraw.timestamp = datetime.datetime.now(),
withdraw.username = username,
withdraw.currency = currency,
withdraw.user_balance = user_balance,
withdraw.withdraw_address = withdraw_address,
withdraw.withdraw_amount = withdraw_amount
withdraw.save()
And here is the views.py file. Whatever I do the error is the following.
...
File "C:\Users\Msi\cover_game\cover\lib\site-packages\django\db\models\fields\__init__.py", line 1554, in to_python
raise exceptions.ValidationError(
django.core.exceptions.ValidationError: ['“(0.011095555563904999,)” value must be a decimal number.']
As you can see with user_balance everything is fine and it's floating number.
CodePudding user response:
“(0.011095555563904999,)”
looks like string value, not float.
You should clean the withdraw_amount
variable by removing braces, quotes and comma.
CodePudding user response:
You can convert it manually using the decimal
module of Python, and use decimal.Decimal()
in both the conditions so:
import decimal
withdraw_amount = decimal.Decimal(request.data['withdraw_amount'])