无法将关键字“用户”解析为字段 - django 错误

Posted

技术标签:

【中文标题】无法将关键字“用户”解析为字段 - django 错误【英文标题】:cannot resolve keyword 'user' into field - django error 【发布时间】:2020-08-06 21:42:49 【问题描述】:

这是我收到的错误消息 --->无法将关键字“用户”解析为字段。选项有:date_joined、email、emailconfirmed、first_name、groups、id、is_active、is_staff、is_superuser、last_login、last_name、logentry、order、password、profile、user_permissions、useraddress、userdefaultaddress、username、userstripenew_order = Order.objects.get(cart=cart) ///// new_order.user = User.objects.get(user=request.user)

我们将不胜感激所有帮助解决这个问题!

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'

【问题讨论】:

【参考方案1】:

也许

**new_order.user = User.objects.get(user=request.user)**

new_order.user = User.objects.get(pk=request.user.pk)

【讨论】:

以上是关于无法将关键字“用户”解析为字段 - django 错误的主要内容,如果未能解决你的问题,请参考以下文章

无法将关键字 u'slug' 解析为 Django 中的字段错误?

无法将关键字'user'解析为字段-Django错误

字段错误在/。无法将关键字“textField”解析为字段

无法将关键字“用户名”解析为字段。选项有:地址、年龄、城市、国家、性别、姓名、电话、pt_id、用户、user_id

无法将关键字“用户”解析为字段。选项有:create_account、email、full_name

Django加入 - 我需要父母和子桌的所有领域