[英]how can I change the user and the value of product(Model) after wallet transaction
Here is my model.py, where Customer is the one to make orders and purchase products.这是我的 model.py,其中 Customer 是下订单和购买产品的人。
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.在views.py中,这里我想用发件人从钱包发送的金额来替换产品的用户(Product.user)。
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:这是我的模板 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在views.py中,这里我想用发件人从钱包发送的金额替换产品的用户(Product.user)
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.此外,如果有人可以 (a) 告诉我管理员如何跟踪交易并 (b) 限制用户如果超出平衡,那就太好了。 Thank you <3谢谢<3
(a) You will need to create a new table Transaction
that will keep track of the exchange between users. (a) 您将需要创建一个新表Transaction
来跟踪用户之间的交换。 Something like (pseudo-Django-Python code):类似于(伪 Django-Python 代码):
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.然后在每笔交易中,创建一个新的Transaction
条目。 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.然后,您可以 (1) 通过 Django 管理门户跟踪交易,或 (2) 创建一个新的仅显示所有交易的管理员页面。
(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,谢谢@Felipe,我确实试过了,
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.在 (a) 中,我想知道如何获取用户之间最近的数据/横断面详细信息。 thank you for (b)<3谢谢(b)<3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.