Django 中的模型关系和查询不清楚

model relationship and Queries in Django not a clear

这些代码行在 Django View 中是什么意思:我找不到详细解释,我从 Laravel 背景来到 Django,所以我可以理解模型和关系...谢谢

customer = request.user.customer
product = Product.objects.get(id=productId)
order, created = Order.objects.get_or_create(customer=customer, complete=False)
orderItem, created = OrderItem.objects.get_or_create(order=order, product=product)
customer = request.user.customer

请求对象有一个用户,该用户是经过身份验证的用户(如果没有用户经过身份验证则返回 AnonymousUser 对象)。在此示例中,User 模型(即用户 table)有一个名为 customer 的字段,我们正在访问该字段。

product = Product.objects.get(id=productId)

在这里,我们只是查询 Product table 以获取具有给定 productId 的特定产品。请注意,如果在使用 .get() 方法时返回两条记录,Django 将引发错误(即,如果 Product table 中的两行具有相同的 productId.

order, created = Order.objects.get_or_create(customer=customer, complete=False)

接下来我们使用 get_or_create() 方法根据 customer 查找 order(我们在上面提取的值. If an order cannot be found we will create one instead. The value of createdwill be True if a neworder` 已创建,如果已经存在则为 False。

orderItem, created = OrderItem.objects.get_or_create(order=order, product=product)

就像上面一样,我们正在使用 orderproduct 字段获取或创建 OrderItem