get_object_or_404(个人资料,用户= request.user)错误

时间:2018-07-09 00:46:58

标签: python django django-queryset profile

我正在尝试为我的网站创建一个购物车,但是当我尝试在功能开始时请求用户个人资料模型时,在我的功能“ add_to_cart”中它告诉我
“用户”对象没有属性“配置文件”

add_to_cart是Checkout views.py中#get用户个人资料下的第一种方法

错误

 AttributeError at /checkout/add-to-cart/2/
'User' object has no attribute 'profile'
Request Method: GET
Request URL:    http://127.0.0.1:8000/checkout/add-to-cart/2/
Django Version: 1.11
Exception Type: AttributeError
Exception Value:    
'User' object has no attribute 'profile'

如果我在个人资料中删除用户的related_name,那么我会收到此错误:

删除related_name时出错

Request Method: GET
Request URL:    http://127.0.0.1:8000/checkout/add-to-cart/2/
Django Version: 1.11
Exception Type: AttributeError
Exception Value:    
'Product' object has no attribute 'all'

结帐views.py

from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required

from django.urls import reverse
from django.shortcuts import render, redirect, get_object_or_404
from users.models import User, Profile, userStripe
from portal.models import Product
#from manaland.helper import get_reference_code

from checkout.extras import generate_order_id
from checkout.models import OrderItem, Order

from django.views import generic
from django.views.generic import CreateView

import stripe
stripe.api_key = settings.STRIPE_SECRET_KEY


def add_to_cart(request, **kwargs):
    #get the user Profile
    user_Profile = get_object_or_404(Profile, user=request.user)

    #filter products for id
    product = Product.objects.filter(id=kwargs.get('item_id', "")).first()

    #check if the user already owns the product
    if product in request.user.profile.merchandise.all():
        messages.info(request, "You already own this product")
        return redirect(reverse('product_list'))
    #create OrderItem of the selected product
    order_item, status = OrderItem.objects.get_or_create(product=Product)
    #create order associate with the user
    user_order, status = Order.objects.get_or_create(owner=user_Profiles, is_ordered=False)
    user_order.items.add(order_item)
    if status:
        #generate a reference code
        user_order.ref_code = generate_order_id()
        user_order.save()

    #show confirmataion message and redirect to same page
    messages.info(request, "item added to cart")
    return redirect(reverse('product_list'))

def delete_from_cart(request, item_id, LoginRequiredMixin):
    item_to_delete = OrderItem.objects.filter(pk=item_id)
    if item_to_delete.exists():
        item_to_delete[0].delete()
        messages.info(request, "Item has been removed from shopping cart")
    return redirect(reverse('shopping_cart:order_summary'))

def order_details(request, LoginRequiredMixin, **kwargs):
    existing_order = get_user_pending_order(request)
    context = {
        'order': existing_order
    }
    return render(request, 'checkout/order_summary', context)


#class UserDetailView(LoginRequiredMixin, DetailView):
#    model = User
    # These next two lines tell the view to index lookups by username
#    slug_field = 'username'
#    slug_url_kwarg = 'username'

def checkout(request, LoginRequiredMixin):
    existing_order = get_user_pending_order(request)
    context = {
        'order': existing_order
    }
    return render(request, 'checkout/checkout', context)


def update_transaction_records(request, order_id):
    #get the order being processed
    order_to_purchase = Order.objects.filter(pk=order_id).first()

    #update the placed order
    order_to_purchase.is_ordered=True
    order_to_purchase.date_ordered=datetime.datetime.now()
    order_to_purchase.save()

    #get all items  in the order
    order_items = order_to_purchase.items.all()

    order_items.update(is_ordered=True, date_ordered=datetime.datetime.now())

    #add products to use profile
    user_Profiles = get_object_or_404(Profile, user=request.user.user_profile)
    #get the product from the items
    order_products = [item.product for item in order_items]
    user_Profiles.merchandise.add(*order_products)
    user_Profiles.save()

    #TODO Update payment records
    #TODO email customer


    messages.info(request, "Thank you! Your items have been added to your account")
    return redirect(reverse('users:detail'))

def success(request, **kwargs):
    return render(request, 'checkout/purchase_success.html', {})

用户模型。py

from django.contrib.auth.models import AbstractUser
from django.core.urlresolvers import reverse
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from allauth.account.signals import user_logged_in, user_signed_up
import stripe
stripe.api_key = settings.STRIPE_SECRET_KEY
from portal.models import Product
#from django.apps import apps
#Product1 = apps.get_model('portal', 'Product')


class User(AbstractUser):
    # First Name and Last Name do not cover name patterns
    # around the globe.
    name = models.CharField(_('Name of User'), blank=True, max_length=255)
    bio = models.CharField( blank=True, max_length=255)
    image = models.ImageField(null=True, blank=True)

    def __str__(self):
        return self.username

    def get_absolute_url(self):
        return reverse('users:detail', kwargs={'username': self.username})

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='user_profile')
    merchandise = models.OneToOneField(Product, blank=True, null=True)
    description = models.CharField( blank=True, max_length=255)
    image = models.ImageField(null=True, blank=True)

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

结帐urls.py

from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url
from . import views

from manaland.views import ProductListView

from .views import (
    add_to_cart,
    delete_from_cart,
    order_details,
    checkout,
    update_transaction_records,
    success
)
#NOTE: https://github.com/codingforentrepreneurs/Guides/blob/master/all/common_url_regex.md

urlpatterns = [

    #Shopping Cart
    url(r'^add-to-cart/(?P<item_id>[-\w]+)/$', views.add_to_cart, name='add_to_cart'),
    url(r'^order-summary/$', order_details, name='order_summary'),
    url(r'^success/$', success, name='purchase_success'),
    url(r'^item/(?P<item_id>[-\w]+)/delete/$', delete_from_cart, name='delete_item'),
    url(r'^checkout/$', checkout, name='checkout'),
    #url(r'^payment/(?P<order_id>[-\w]+)/update/$', process_payment, name='process_payment'),
    url(r'^update-transaction/(?P<order_id>[-\w]+)/$', update_transaction_records, name='update_records'),


    #url(r'^tip/create/$', views.TipCreate.as_view(), name='tip_create'),

]


if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

1 个答案:

答案 0 :(得分:1)

您的个人资料模型通过user_profile相关名称与您的用户模型相关,因此正确的查询方式是:

...
if product in request.user.user_profile.merchandise.all():
    ...

如果出于任何原因要重命名此关系,则:

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='profile')

然后,您的merchandise关系是一对一的,因此它将返回一个对象,而不是一个集合,不需要全部:

...
if product in request.user.user_profile.merchandise:
    ...

您可以在此处进行的一项改进是,由于商品是另一个对象,因此该查找将再次访问数据库,以查看商品是否存在。为了节省您的数据库命中率,可以使用“配置文件”自动字段merchandise_id来读取保存在“配置文件”表中的信息:

...
if product in request.user.user_profile.merchandise_id:
    ...

每个关系(ForeignKey,ManyToMany和OneToOne)都会在转发模型中创建这些自动字段,因此可以轻松地查询相关对象是否存在而不必调用它。

我知道您很难理解Django ORM的所有概念,所以我建议您通读this documentation页,它涵盖了成功Django的所有基本关系和概念。项目。