无法将关键字'user'解析为字段-Django错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法将关键字'user'解析为字段-Django错误相关的知识,希望对你有一定的参考价值。
这是我得到的错误消息--->无法将关键字'user'解析为字段。选项包括:date_joined,电子邮件,已确认电子邮件,first_name,组,id,is_active,is_staff,is_superuser,last_login,last_name,logentry,顺序,密码,配置文件,user_permissions,useraddress,userdefaultaddress,username,usertripe new_order = Order.objects.get(购物车=购物车) ///// new_order.user = User.objects.get(用户=请求用户)
在解决此问题时,所有帮助将不胜感激!
views.py-订单
import time
import stripe
from django.contrib.auth.decorators import login_required
from django.conf import settings
from django.contrib.auth.models import User
from django.shortcuts import render, HttpResponseRedirect
from django.urls import reverse
# Create your views here.
from users.forms import UserAddressForm
from carts.models import Cart
from .models import Order
from users.models import Profile, UserAddress, UserAddressManager
from .utils import id_generator
stripe.api_key = settings.STRIPE_SECRET_KEY
def orders(request):
context = {}
template = "orders/user.html"
return render(request, template, context)
@login_required
def checkout(request):
try:
the_id = request.session['cart_id']
cart = Cart.objects.get(id=the_id)
except:
the_id = None
return HttpResponseRedirect(reverse("cart"))
try:
**new_order = Order.objects.get(cart=cart)**
except Order.DoesNotExist:
new_order = Order()
new_order.cart=cart
**new_order.user = User.objects.get(user=request.user)**
new_order.order_id = id_generator()
new_order.final_total = cart.total
new_order.save()
except:
return HttpResponseRedirect(reverse("cart"))
try:
address_added = request.GET.get("address_added")
except:
address_added = None
if address_added is None:
address_form = UserAddressForm()
else:
address_form = None
current_addresses = UserAddress.objects.filter(user=request.user)
billing_addresses = UserAddress.objects.get_billing_addresses(user=request.user)
if new_order.status == "Finished":
del request.session['cart_id']
del request.session['items_total']
return HttpResponseRedirect(reverse("cart"))
key = settings.STRIPE_PUBLISHABLE_KEY
context = {
"order": new_order,
"address_form": address_form,
"current_addresses": current_addresses,
"billing_addresses": billing_addresses,
"key": key,
}
template = "orders/checkout.html"
return render(request, template, context)
models.py -orders
from django.db import models
# Create your models here.
from carts.models import Cart
from django.conf import settings
from django.contrib.auth.models import User
STATUS_CHOICES =(
("Started", "Started"),
("Abandoned", "Abandoned"),
("Finished", "Finished"),
)
class Order(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE)
order_id = models.CharField(max_length=120, default='ABC', unique=True)
cart = models.ForeignKey('carts.Cart', on_delete=models.CASCADE)
status = models.CharField(max_length=120, choices=STATUS_CHOICES, default="Started")
final_total = models.DecimalField(default=10.99, max_digits=1000, decimal_places=2)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __string__(self):
return self.order_id
models.py-用户
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default='', upload_to='profile_pics')
def __str__(self):
return f'{self.user.username} Profile'
答案
也许
**new_order.user = User.objects.get(user=request.user)**
new_order.user = User.objects.get(pk=request.user.pk)
以上是关于无法将关键字'user'解析为字段-Django错误的主要内容,如果未能解决你的问题,请参考以下文章
无法将关键字“用户”解析为字段。选项有:create_account、email、full_name
Django模板如何修复错误“无法解析余数:'|'来自'|' ”
如何修复:无法将关键字“用户”解析为字段。选项有:description、end_time、id、start_time、title Django 错误