【问题标题】:I want to create a relational Database in Django with user_auth table which is created automatically in postgreSQL我想在 Django 中使用在 postgreSQL 中自动创建的 user_auth 表创建一个关系数据库
【发布时间】:2021-09-03 22:17:59
【问题描述】:

我想创建一个带有 auth_user 表的关系数据库,该表在迁移后在 django 中自动创建(数据库是 postgresql)。我创建了一个名为 order 的类的表。我的models.py 是这样的

模型.py

from django.db import models
from django.contrib.auth.models import User
   # Create your models here.

class order(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    product = models.CharField(max_length=50)
    price = models.IntegerField()

现在我想用 user_id 将用户 ID 与表顺序链接起来。我的views.py 看起来像这样。 视图.py

def order_Data(request):
    product = request.POST['product']
    price = request.POST['price']


    orders = order(product=product,price=price)


    orders.save()
    messages.info(request,'Data saved')
    return render(request,'home.html')

我认为我已正确链接表,但在订单表的 user_id 中设置为 null 并给我错误。我不知道现在该怎么办。请帮帮我(我也是 Django 的学习者)。提前谢谢你。

【问题讨论】:

    标签: python django


    【解决方案1】:

    您没有将用户传递给订单

    orders = order(product=product,price=price, user=request.user)
    

    假设您有一个登录用户

    【讨论】:

    • 就这样????有这么简单吗??好的,非常感谢。
    • 非常感谢您,先生.....这工作......你解决了我的问题
    • 请接受答案以关闭问题。
    猜你喜欢
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 2013-03-15
    相关资源
    最近更新 更多