Error: User has no customer.

Hey everybody,

I found some information in the forum but I am not sure if the information is for my issue.

I have an issue on the cart page. I want to loop through the orders and items and display all of the items on the cart page. In the beginning the cart page is working but the loop is not working. After some time I get an error message:

I understand what it means, I think so, but I do not know where I should add the ‘customer’ and how.

first more code:

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()
		#Create empty cart for now for non-logged in user
		items = []
	return render(request, 'store/cart.html', {'items': items}) (not sure if all models are necessary but…)

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Customer(models.Model):
	user = models.OneToOneField(User, null=True, blank=True, related_name = 'customer', on_delete=models.CASCADE)
	name = models.CharField(max_length=200, null=True)
	email = models.CharField(max_length=200, null=True)

	def __str__(self):
		return  # the name will be visible in admin panel

class Product(models.Model):
	name = models.CharField(max_length=200)
	price = models.FloatField()
	digital = models.BooleanField(default=False, null=True, blank=True)
	image = models.ImageField(null=True, blank=True)

	def __str__(self):

	# if no image is uploaded to the Product, no error raised:
	def imageURL(self):
			url = self.image.url
			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=100, null=True)

	def __str__(self):
		return str(

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)

class ShippingAddress(models.Model):
	customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True)
	order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
	address = models.CharField(max_length=200, null=True)
	city = 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

So, if I understand this code customer = request.user.customer and the error: User has no customer. wants to tell me that in the User model there is no customer. I do not know what I should do with that information. Please help me?

If I forgot to post something please let me know.

Thank you

Add the code to create the Customer object for that user if one doesn’t currently exist.

I do not understand what you mean?
Which code do you mean should I create?

Do you mean in the page function, I have to add code to create the Customer object for the user?

Yes. Django isn’t going to create it for you.

That’s one place it could be done.

You could also add it to whatever page you’re using to create the User object such that when a new User is added, the Customer object is also created at the same time.

1 Like