'RelatedObjectDoesNotExist: User has no customer
I am following a Django youtube E-comerce tutorial by Dennis Ivy. I follow the tutorial line by line. from the initial i use to run the program and view all the output but alone the way i start getting this error message "RelatedObjectDoesNotExist: User has no customer". Am confuse because i keep repeating the video to see where i was wrong but could not trace it. please what is the solution? I realise the last time i tried, it open on its own using 'internet explorer' it display. when i open in 'chrome', it show the same erro. but when i deleted a customer from database it start showing the same erro again in 'internet explorer'. I dont want to give up on the tutorial cos i need it. please someone should assist me. Thanks
from multiprocessing import context
from django.shortcuts import render
from .models import *
from django.contrib.auth.models import User
from django.http import JsonResponse
import json
# Create your views here.
def store(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created=Order.objects.get_or_create(customer=customer, complete =False)
items =order.orderitem_set.all()
cartItems=order.get_cart_items
else:
items=[]
order = {'get_cart_total':0, 'get_cart_items':0}
cartItems=order['get_cart_items']
products = Product.objects.all()
context = {
'products':products,
'cartItems':cartItems,
}
return render(request, 'store/store.html', context)
def cart(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created=Order.objects.get_or_create(customer=customer, complete =False)
items =order.orderitem_set.all()
cartItems=order.get_cart_items
else:
items=[]
order = {'get_cart_total':0, 'get_cart_items':0}
cartItems=order['get_cart_items']
context = {
'items':items,
'order':order,
'cartItems':cartItems,
}
return render(request, 'store/cart.html', context)
def checkout(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created=Order.objects.get_or_create(customer=customer, complete =False)
# order, created=Order.objects.get_or_create(complete =False)
items =order.orderitem_set.all()
else:
items=[]
order = {'get_cart_total':0, 'get_cart_items':0}
cartItems=order['get_cart_items']
context = {
'items':items,
'order':order,
'cartItems':cartItems
}
return render(request, 'store/checkout.html', context)
def updateItem(request):
data = json.loads(request.data)
productId = data['productId']
action = data['action']
print('Action:', action)
print('productId:', productId)
customer=request.user.customer
product=Product.objects.get(id=productId)
order, created=Order.objects.get_or_create(complete =False)
orderItem, created=OrderItem.objects.get_or_create(order=order, product=product)
if action =='add':
orderItem.quantity =(orderItem.quantity +1)
elif action =='remove':
orderItem.quantity =(orderItem.quantity -1)
orderItem.save()
if orderItem.quantity <=0:
orderItem.delete()
return JsonResponse('Item was added', safe =False)
this is the models.py
import email
from django.db import models
from django.db.models.fields.related import OneToOneField
from django.contrib.auth.models import User
from numpy import product
# Create your models here.
class Customer(models.Model):
user=models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True)
email=models.CharField(max_length=200)
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=200, null=True)
price = models.FloatField()
digital=models.BooleanField(default=False, null=True, blank=True)
image = models.ImageField(null=True, blank=True)
def __str__(self):
return self.name
#this is to avoid erro in the template when we delete image
@property
def imageURL(self):
try:
url=self.image.url
except:
url=''
return url
class Order(models.Model):
customer=models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
date_ordered=models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False)
transaction_id = models.CharField(max_length=200, null=True)
def __str__(self):
return str(self.id)
@property
def get_cart_total(self):
orderitems = self.orderitem_set.all()
total = sum([item.get_total for item in orderitems])
return total
@property
def get_cart_items(self):
orderitems = self.orderitem_set.all()
total = sum([item.quantity for item in orderitems])
return total
class OrderItem(models.Model):
product=models.ForeignKey(Product, on_delete=models.SET_NULL, null=True, blank=True)
order=models.ForeignKey(Order, on_delete=models.SET_NULL, null=True, blank=True)
quantity = models.IntegerField(default=0, null=True, blank=True)
date_added=models.DateTimeField(auto_now_add=True)
@property
def get_total(self):
total = self.product.price * self.quantity
return total
class ShippingAddress(models.Model):
customer=models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
order=models.ForeignKey(Order, on_delete=models.SET_NULL, null=True, blank=True)
address= models.CharField(max_length=200, null=True)
city = models.CharField(max_length=200, null=True)
state = models.CharField(max_length=200, null=True)
zipcode = models.CharField(max_length=200, null=True)
date_added=models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.address
this is the urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.store, name = 'store'),
path('cart/', views.cart, name = 'cart'),
path('checkout', views.checkout, name = 'checkout'),
path('update_item/', views.updateItem, name='update_item')
]
Solution 1:[1]
you can use the request.user to query the customer
customer = Customer.objects.get(user__id = request.user.id)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sherif Hassan |
