python - 如何从与当前登录用户关联的模型导入和编辑变量

标签 python django python-3.x django-models

我想从与当前登录用户关联的模型导入变量。我的模型与内置用户模型使用一对一的关系。导入变量后,我想针对当前登录的任何用户将其更改为 True。

我尝试弄乱我的 import 语句并让它正常工作,当我尝试编辑变量时,它给了我“PurchaseHistory() 得到了意外的关键字参数 'id'”。我知道这是我对网上找到的代码片段的误解,我只是希望有人帮助我修复它。

#models.py
class PurchaseHistory(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    InvestingOne = models.BooleanField(default=False)

    def __str__(self):
        return self.user.username

    class Meta:
        abstract = True
        app_label = 'PurchaseHistory'
#payments/views.py
def charge(request):
    if request.method == 'POST':
        charge = stripe.Charge.create(
            amount=500,
            currency='usd',
            description='A Django charge',
            source=request.POST['stripeToken']
        )
        post = PurchaseHistory(id=InvestingOne)
        post.InvestingOne = True
        post.save()
        return render(request, 'charge.html')

我相信我提供了很好的信息来帮助我解决这个问题。如果我需要提供任何其他详细信息,请告诉我。谢谢。

编辑:这是我的文件结构。我认为唯一可能使问题复杂化的是我的模型位于主项目文件夹中,而我的 View 位于名为 payment 的应用程序中。 [img]/image/W6AeL.png

最佳答案

首先,您的模型应如下所示

class PurchaseHistory(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="purchase_history")
    is_one_invested = models.BooleanField(default=False)

    def __str__(self):
        return self.user.username

    class Meta:
        app_label = 'PurchaseHistory'

我看到你有抽象模型,你不能在查询中使用它 https://docs.djangoproject.com/en/2.2/topics/db/models/#abstract-base-classes

进行迁移并迁移并走得更远

#payments/views.py
def charge(request):
    if request.method == 'POST':
        charge = stripe.Charge.create(
            amount=500,
            currency='usd',
            description='A Django charge',
            source=request.POST['stripeToken']
        )
        if request.user.is_authenticated:
            if PurchaseHistory.objects.filter(user=request.user).exists():
                request.user.purchase_history.is_one_invested = True
                request.user.purchase_history.save()

        return render(request, 'charge.html')

或者如果您想创建对象而不是更新

#payments/views.py
def charge(request):
    if request.method == 'POST':
        charge = stripe.Charge.create(
            amount=500,
            currency='usd',
            description='A Django charge',
            source=request.POST['stripeToken']
        )
        if request.user.is_authenticated:
            PurchaseHistory.objects.create(user=request.user, is_one_invested=True)

        return render(request, 'charge.html')

在 Django <= 1.9 中使用 is_authenticated() 而不是 is_authenticated

关于python - 如何从与当前登录用户关联的模型导入和编辑变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57600056/

相关文章:

iphone - 在 iPhone 上测试 Django 网站

python-3.x - pyhash模块无法用pip安装

python - 避免自动转换为 float python

python - 根据 pandas 中的另一个列值突出显示一个列值

python - 如何让文字显示5秒然后消失并显示按钮?

java - 使用不同的解释器执行shell脚本

django - Django Rest Framework: 'function'对象没有属性 'as_view'

python - Django:传输/访问表单完整错误消息

python-3.x - python3.PyImport_ImportModule(name) 在第二次调用时会发出 fatal error

python-3.x - UnexpectedTagNameException : Message: Select only works on <select> elements, 不是 <li> 使用 Selenium 从下拉列表中选择 li 元素时出错