Here is my model.py, where Customer is the one to make orders and purchase products.
class Customer(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=25, null=True)
phone = models.CharField(max_length=12, null=True)
def __str__(self):
return self.name
class Product(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
coinid = models.CharField(max_length=255, null=True)
digit = models.CharField(max_length=18, null=True)
ctp = models.FloatField(max_length=100, null=True)
transection_id = models.IntegerField(null=True, default=0)
slug = models.SlugField(max_length=250, null=True, blank=True)
date_created = models.DateField(auto_now_add=True, null=True)
def __str__(self):
return self.coinid
def get_absolute_url(self):
return reverse("core:detail", kwargs={
'slug': self.coinid
})
class Balance(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
balance = models.IntegerField(default=0)
def __str__(self):
return self.user.username
In views.py, Here I want to replace the user (Product.user) of the product by sender sending amount from wallet.
def buy_c(request, ok):
ctp_val = Product.objects.get(id=ok)
msg = "Enter Details"
if request.method == "POST":
try:
username = request.POST["username"]
amount = request.POST["amount"]
senderUser = User.objects.get(username=request.user.username)
receiverrUser = User.objects.get(username=username)
sender = Balance.objects.get(user=senderUser)
receiverr = Balance.objects.get(user=receiverrUser)
sender.balance = sender.balance - float(amount)
receiverr.balance = receiverr.balance + float(amount)
if senderUser == receiverrUser:
return Exception.with_traceback()
else:
sender.save()
receiverr.save()
msg = "Transaction Success"
return redirect("/user")
except Exception as e:
print(e)
msg = "Transaction Failure, Please check and try again"
context = {"coin": ctp_val, "msg":msg}
return render(request, 'coinwall.html', context)
Here is my template coinwall.html:
<tr>
<th>username</th>
<th>Coin Id</th>
<th>current CTP</th>
<th>Total Transections</th>
</tr>
<tr>
<td>{{coin.user}}</td>
<td>{{coin.coinid}}</td>
<td>{{coin.ctp}}</td>
<td>{{coin.transection_id}}</td>
</tr>
{% if msg %}
<br><br>
{{msg}}
{% endif %}
<form method="POST">
<input type="text" name="username" value="{{coin.user}}" readonly><br><br>
<input type="text" name="amount" value="{{coin.ctp}}" readonly><br><br>
<input type="submit" value="Submit">
{% csrf_token %}
</form>
Also it will be great if someone can tell me how the admin can trace the transactions and restrict the user if out of Balance.
in views.py, Here I want to replace the user(Product.user) of the product by sender sending amount from wallet
ctp_val.update(user=receiverrUser)
Also, it will be great if someone can (a) tell me how the admin can trace the transactions and (b) restrict the user If out of Balance. Thank you <3
(a) You will need to create a new table Transaction
that will keep track of the exchange between users. Something like (pseudo-Django-Python code):
class Transaction:
id = random_generated_hash()
timestamp = time.now()
buyer = User
seller = User
product = Product
price = float
And then on every transaction, create a new Transaction
entry. You can then either (1) keep track of transactions via the Django admin portal, or (2) create an new admin-only page that shows all of the transactions.
(b)
recvbalance = Balance.objects.filter(user=receiverrUser).first()
if recvbalance and recvbalance.balance >= ctp_val.price: # Whatever is the key for 'product price'.
# Do exchange here.
else:
# Throw error.
thank you @Felipe, I did tried,
ctp_val.update(user=receiverrUser)
Product value has to attribute 'update'
then I tried;
ctp_val.user = senderUser
ctp_val.save()
and it worked for me
In (a) I want to know, how I can get recent data/transection details between users. thank you for (b)<3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.