简体   繁体   English

钱包交易后如何更改用户和产品(型号)的价值

[英]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.

相关问题 如何将比特币纸质钱包密钥导入bitcoinlib钱包? - How can I import a Bitcoin paper wallet key into a bitcoinlib wallet? 如何使用方法自动更改 django model 字段值? - How can I change django model field value automatically with method? 如何在 django 用户 model 中生成随机唯一用户 ID 值并将其显示在 django 管理员上? - How can I generate a random unique User id value in django user model and show it on django admin? 如何为 Django 产品 model 使用多重图像? - How can I use multible images for Django product model? 在 Django 模型中执行算术运算后,如何使用获得的新值更新模型中整数的值? - After performing arithmetic operations within a django model, how can I update the value of an integer in my model using the new value I obtained? 在Django中扩展用户模型后,我无法登录 - After extending the User model in django, I can not log in 选择特定选项后,如何更改 Django model 字段的值? - How can I change the value of a Django model field when a particular choice is selected? 如何在 django 中对产品进行星级评分,如何将其存储在 model 中并将其呈现为模板? - How can I take star rating of product in django and how can I store it in model and render it to template? 我如何找到一个投影来保留内部产品的相对价值? - How can I find a projection to preserve the relative value of inner product? 用户单击后如何更改选项菜单的值? - How to change the value of an option menu after user clicks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM