new price is the price taken from the marketcapapi. I would like that every time the user buys coins, the coins are transferred to the ALGO_wallet leaving n_ALGO_coin at zero (= 0). The first time the user buys, everything is ok, the transfer is done, while the second time the data remains both in n_ALGO_coin and also in ALGO_wallet. I tried with a for loop but it tells me that the object is no interable
def buyalgomkt(request):
new_price = algoValue()
if request.method == "POST":
form = PurchaseForm(request.POST)
if form.is_valid():
form.save(commit=False)
profile = Profile.objects.get(user=request.user)
if profile.USD_wallet > 0 and form.instance.max_spend_usd < profile.USD_wallet:
form.instance.purchased_coin = form.instance.max_spend_usd / form.instance.price_mkt
profile.n_ALGO_coin += form.instance.purchased_coin
profile.USD_wallet -= form.instance.max_spend_usd
profile.ALGO_Wallet += profile.n_ALGO_coin
profile.n_ALGO_coin = profile.ALGO_Wallet - profile.n_ALGO_coin
form.save()
profile.save()
name = request.user
name.save()
messages.success(request, 'Your order has been placed and processed')
else:
messages.warning(request, 'You do not have enough money')
return redirect('purchase')
else:
return HttpResponseBadRequest()
else:
form = PurchaseForm()
return render(request, 'coinmarketalgo/purchase.html', {'form': form, 'new_price': new_price})
.. how can I do?
class Profile(models.Model):
_id = ObjectIdField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
ips = models.Field(default=[])
subprofiles = models.Field(default={})
n = random.randint(1, 10)
n_ALGO_coin = models.FloatField(default=algoValue() * n)
ALGO_Wallet = models.FloatField(default=0)
purchased_coin = models.FloatField(default=0)
USD_wallet = models.FloatField(default=10000)
profit = models.FloatField(default=0)
class Purchase(models.Model):
_id = ObjectIdField()
max_spend_usd = models.FloatField(default=0)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='purchases')
price_mkt = models.FloatField(default=algoValue())
datetime = models.DateTimeField(auto_now_add=True)
purchased_coin = models.FloatField(default=0)
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='profile')