title: Django - store.models.Customer.MultipleObjectsReturned: get() returned more than one Customer -- it returned 2"
body: <p>I am trying to make an e-commerce website where &quot;AnonymousUser&quot; or Guest user can order and check out products by providing their name, email, and address. But after clicking the &quot;Make Payment&quot; button, my terminal was having an error that says &quot;store.models.Customer.MultipleObjectsReturned: get() returned more than one Customer -- it returned 2!&quot;</p>
  <p>When I try to login and do the process for an authenticated user, it doesn't have an error. It just happen to AnonymousUsers.</p>
  <p><strong>Here's my checkout.html:</strong></p>
  <pre><code>{% extends 'store/main.html' %}
  {% load static %}
  {% block content %}
       &lt;div class=&quot;row&quot;&gt;
          &lt;div class=&quot;col-lg-6&quot;&gt;
              &lt;div class=&quot;box-element&quot; id=&quot;form-wrapper&quot;&gt;
                  &lt;form id=&quot;form&quot;&gt;
                      &lt;div id=&quot;user-info&quot;&gt;
                          &lt;div class=&quot;form-field&quot;&gt;
                              &lt;input required class=&quot;form-control&quot; type=&quot;text&quot; name=&quot;name&quot; placeholder=&quot;Name..&quot;&gt;
                          &lt;/div&gt;
                          &lt;div class=&quot;form-field&quot;&gt;
                              &lt;input required class=&quot;form-control&quot; type=&quot;email&quot; name=&quot;email&quot; placeholder=&quot;Email..&quot;&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      
                      &lt;div id=&quot;shipping-info&quot;&gt;
                          &lt;hr&gt;
                          &lt;p&gt;Shipping Information:&lt;/p&gt;
                          &lt;hr&gt;
                          &lt;div class=&quot;form-field&quot;&gt;
                              &lt;input class=&quot;form-control&quot; type=&quot;text&quot; name=&quot;address&quot; placeholder=&quot;Address..&quot;&gt;
                          &lt;/div&gt;
                          &lt;div class=&quot;form-field&quot;&gt;
                              &lt;input class=&quot;form-control&quot; type=&quot;text&quot; name=&quot;city&quot; placeholder=&quot;City..&quot;&gt;
                          &lt;/div&gt;
                          &lt;div class=&quot;form-field&quot;&gt;
                              &lt;input class=&quot;form-control&quot; type=&quot;text&quot; name=&quot;state&quot; placeholder=&quot;State..&quot;&gt;
                          &lt;/div&gt;
                          &lt;div class=&quot;form-field&quot;&gt;
                              &lt;input class=&quot;form-control&quot; type=&quot;text&quot; name=&quot;zip&quot; placeholder=&quot;zip code..&quot;&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;hr&gt;
                      &lt;input id=&quot;form-button&quot; class=&quot;btn btn-success btn-block&quot; type=&quot;submit&quot; value=&quot;Continue&quot;&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
  
              &lt;br&gt;
              &lt;div class=&quot;box-element hidden&quot; id=&quot;payment-info&quot;&gt;
                  &lt;small&gt;Paypal Options&lt;/small&gt;
                  &lt;button id=&quot;make-payment&quot;&gt;Make Payment&lt;/button&gt;
              &lt;/div&gt;
              
          &lt;/div&gt;
  
          &lt;div class=&quot;col-lg-6&quot;&gt;
              &lt;div class=&quot;box-element&quot;&gt;
                  &lt;a  class=&quot;btn btn-outline-dark&quot; href=&quot;{% url 'cart' %}&quot;&gt;&amp;#x2190; Back to Cart&lt;/a&gt;
                  &lt;hr&gt;
                  &lt;h3&gt;Order Summary&lt;/h3&gt;
                  &lt;hr&gt;
                  {% for item in items %}
                      &lt;div class=&quot;cart-row&quot;&gt;
                          &lt;div style=&quot;flex:2&quot;&gt;&lt;img class=&quot;row-image&quot; src=&quot;{{item.product.imageURL}}&quot;&gt;&lt;/div&gt;
                          &lt;div style=&quot;flex:2&quot;&gt;&lt;p&gt;{{item.product.name}}&lt;/p&gt;&lt;/div&gt;
                          &lt;div style=&quot;flex:1&quot;&gt;&lt;p&gt;₱{{item.product.price|floatformat:2}}&lt;/p&gt;&lt;/div&gt;
                          &lt;div style=&quot;flex:1&quot;&gt;&lt;p&gt;x{{item.quantity}}&lt;/p&gt;&lt;/div&gt;
                      &lt;/div&gt;
                  {% endfor %}
                  &lt;h5&gt;Items:   {{order.get_cart_items}}&lt;/h5&gt;
                  &lt;h5&gt;Total:   ₱{{order.get_cart_total|floatformat:2}}&lt;/h5&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  &lt;script type=&quot;text/javascript&quot;&gt;
      var shipping = '{{order.shipping}}'
      var user = '{{request.user.is_authenticated}}'
      var total = '{{order.get_cart_total|floatformat:2}}'
      
  
      if(shipping == 'False'){
          document.getElementById('shipping-info').innerHTML = ''
      }
  
      if (user == &quot;True&quot;){
          document.getElementById('user-info').innerHTML = ''
      }
  
      if (shipping == 'False' &amp;&amp; user == &quot;True&quot;){
          //Hide entire form if user is logged in and shipping is false
          document.getElementById('form-wrapper').classList.add('hidden');
          //Show payment if logged in user wants to buy an item that does not require shipping
          document.getElementById('payment-info').classList.remove('hidden');
      }
  
      var form = document.getElementById('form')
      
      form.addEventListener('submit', function(e){
          e.preventDefault()
          console.log('Form Submitted...')
          document.getElementById('form-button').classList.add(&quot;hidden&quot;);
          document.getElementById('payment-info').classList.remove(&quot;hidden&quot;);
      })
  
      document.getElementById('make-payment').addEventListener('click', function(e){
          submitFormData()
      })
  
      function submitFormData(){
          console.log('Payment button clicked')
  
          var userFormData = {
              'name':null,
              'email':null,
              'total':total,
          }
  
          var shippingInfo = {
              'address':null,
              'city':null,
              'state':null,
              'zip':null,
          }
  
          if (shipping != 'False'){
              shippingInfo.address = form.address.value
              shippingInfo.city = form.city.value
              shippingInfo.state = form.state.value
              shippingInfo.zip = form.zip.value
          }
  
          if (user == 'False'){
              userFormData.name = form.name.value
              userFormData.email = form.email.value
          }
  
          var url = '/process_order/'
          fetch(url,{
              method:'POST',
              headers:{
                  'Content-Type':'application/json',
                  'X-CSRFToken':csrftoken,
              },
              body:JSON.stringify({'form':userFormData, 'shipping':shippingInfo})
          })
  
          .then((response) =&gt; response.json())
          .then((data) =&gt; {
              console.log('Success:', data);
              alert('Transaction Completed');
  
              cart ={}
              document.cookie = 'cart=' + JSON.stringify(cart) + &quot;;domain=;path=/&quot;
  
              window.location.href = &quot;{% url 'store' %}&quot;
          })
           
      }
  
  &lt;/script&gt;
  
  {% endblock content %}
  </code></pre>
  <p><strong>Here's my models.py:</strong></p>
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  
  # Create your models here.
  
  class Customer(models.Model):
      user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
      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)
      price = models.DecimalField(max_digits=9, decimal_places=2)
      digital = models.BooleanField(default=False, null=True, blank=True)
      image = models.ImageField(null=True, blank=True)
  
      def __str__(self):
          return self.name
  
      @property
      def imageURL(self):
          try:
              url = self.image.url 
          except:
              url = ''
          return url
  
  class Order(models.Model):
      customer = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True, blank=True)
      date_ordered = models.DateTimeField(auto_now_add=True)
      complete = models.BooleanField(default=False)
      transaction_id = models.CharField(max_length=20, null=True)
  
      def __str__(self):
          return str(self.id)
  
      @property
      def shipping(self):
          shipping = False
          orderitems = self.orderitem_set.all()
          for i in orderitems:
              if i.product.digital == False:
                  shipping = True
          return shipping
  
      @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)
      order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=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)
      order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
      address = models.CharField(max_length=200, null=False)
      city = models.CharField(max_length=200, null=False)
      state = models.CharField(max_length=200, null=False)
      zip = models.CharField(max_length=200, null=False)
      date_added = models.DateTimeField(auto_now_add=True)
  
      def __str__(self):
          return self.address
  </code></pre>
  <p><strong>Here's my views.py:</strong></p>
  <pre><code>from django.http import JsonResponse
  from django.shortcuts import render
  from .models import *
  import json
  import datetime
  from .utils import cookieCart, cartData, guestOrder
  
  def store(request): 
  
      data = cartData(request)
      cartItems = data['cartItems']
  
      products = Product.objects.all()
      context = {'products':products, 'cartItems': cartItems}
      return render(request, 'store/store.html', context)
  
  def cart(request):
  
      data = cartData(request)
      cartItems = data['cartItems']
      order = data['order']
      items = data['items']
  
      context = {'items':items, 'order':order, 'cartItems': cartItems}
      return render(request, 'store/cart.html', context)
  
  def checkout(request):
      data = cartData(request)
      cartItems = data['cartItems']
      order = data['order']
      items = data['items']
  
      context = {'items':items, 'order':order, 'cartItems': cartItems}
      return render(request, 'store/checkout.html', context)
  
  def updateItem(request):
      data = json.loads(request.body)
      productId = data['productId']
      action = data['action']
      print('Action:', action)
      print('Product:', productId)
  
      customer = request.user.customer
      product = Product.objects.get(id=productId)
      order, created = Order.objects.get_or_create(customer=customer, 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 &lt;= 0:
          orderItem.delete()
  
      return JsonResponse('Item was added', safe=False)
  
  def processOrder(request):
      transaction_id = datetime.datetime.now().timestamp()
      data = json.loads(request.body)
  
      if request.user.is_authenticated:
          customer = request.user.customer
          order, created = Order.objects.get_or_create(customer=customer, complete=False)
  
      else:
          customer, order = guestOrder(request, data)
  
      total = float(data['form']['total'])
      order.transaction_id = transaction_id
  
      if total == order.get_cart_total:
          order.complete = True
      order.save()
  
      if order.shipping == True:
          ShippingAddress.objects.create(
              customer=customer,
              order=order,
              address=data['shipping']['address'],
              city=data['shipping']['city'],
              state=data['shipping']['state'],
              zip=data['shipping']['zip'],
          )
  
      return JsonResponse('Payment complete!', safe=False)
  </code></pre>
  <p><strong>And here's my utils.py:</strong></p>
  <pre><code>import json
  from .models import *
  
  def cookieCart(request):
      try:
          cart = json.loads(request.COOKIES['cart'])
      except:
          cart = {}
      
      print('Cart:', cart)
      items = []
      order = {'get_cart_total':0, 'get_cart_items':0, 'shipping': False}
      cartItems = order['get_cart_items']
  
      for i in cart:
          try:
              cartItems += cart[i][&quot;quantity&quot;]
  
              product = Product.objects.get(id=i)
              total = (product.price * cart[i]['quantity'])
  
              order['get_cart_total'] += total
              order['get_cart_items'] += cart[i]['quantity']
  
              item = {
                  'product':{
                      'id':product.id,
                      'name':product.name,
                      'price':product.price,
                      'imageURL':product.imageURL,
                      },
                  'quantity': cart[i]['quantity'],
                  'get_total': total,
                  }
              items.append(item)
  
              if product.digital == False:
                  order['shipping'] = True
  
          except:
              pass
      return {'cartItems': cartItems, 'order': order, 'items': items}
  
  def cartData(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:
          cookieData = cookieCart(request)
          cartItems = cookieData['cartItems']
          order = cookieData['order']
          items = cookieData['items']
  
      return {'cartItems': cartItems, 'order': order, 'items': items}
  
  def guestOrder(request, data):
      print('User is not logged in...')
  
      print('COOKIES:', request.COOKIES)
      name = data['form']['name']
      email = data['form']['email']
  
      cookieData = cookieCart(request)
      items = cookieData['items'] 
  
      customer, created = Customer.objects.get_or_create(
          email=email,
          )
      customer.name = name
      customer.save()
  
      order = Order.objects.create(
          customer=customer,
          complete=False,
          )
  
      for item in items:
          product=Product.objects.get(id=item['product']['id'])
  
          orderItem = OrderItem.objects.create(
              product=product,
              order=order,
              quantity=item['quantity']
              )
  
      return customer, order
  </code></pre>
  <p>How can I get rid of that error and let guest users check out without the need to make an account or login?</p>
  <p>EDITED:</p>
  <p>When I check all the info I entered in the checkout from in the admin site, I encountered an error that says &quot;<strong>str</strong> returned non-string (type NoneType)&quot;</p>
  <p>Here's the problem:</p>
  <pre><code>TypeError at /admin/store/customer/3/change/
  __str__ returned non-string (type NoneType)
  Request Method: GET
  Request URL:    http://127.0.0.1:8000/admin/store/customer/3/change/
  Django Version: 4.0.2
  Exception Type: TypeError
  Exception Value:    
  __str__ returned non-string (type NoneType)
  Exception Location: C:\Users\RolfShin025\Desktop\E-COMMERCE\env\lib\site-packages\django\contrib\admin\options.py, line 1645, in _changeform_view
  Python Executable:  C:\Users\RolfShin025\Desktop\E-COMMERCE\env\Scripts\python.exe
  Python Version: 3.10.2
  Python Path:    
  ['C:\\Users\\RolfShin025\\Desktop\\E-COMMERCE\\WannaCome',
   'C:\\Program Files\\Python310\\python310.zip',
   'C:\\Program Files\\Python310\\DLLs',
   'C:\\Program Files\\Python310\\lib',
   'C:\\Program Files\\Python310',
   'C:\\Users\\RolfShin025\\Desktop\\E-COMMERCE\\env',
   'C:\\Users\\RolfShin025\\Desktop\\E-COMMERCE\\env\\lib\\site-packages']
  Server time:    Sat, 26 Feb 2022 19:59:54 +0000
  </code></pre>
  <p>Here's the traceback in my Terminal:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\RolfShin025\Desktop\E-COMMERCE\env\lib\site-packages\django\core\handlers\exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;C:\Users\RolfShin025\Desktop\E-COMMERCE\env\lib\site-packages\django\core\handlers\base.py&quot;, line 181, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;C:\Users\RolfShin025\Desktop\E-COMMERCE\env\lib\site-packages\django\contrib\admin\options.py&quot;, line 622, in wrapper
      return self.admin_site.admin_view(view)(*args, **kwargs)
    File &quot;C:\Users\RolfShin025\Desktop\E-COMMERCE\env\lib\site-packages\django\utils\decorators.py&quot;, line 130, in _wrapped_view
      response = view_func(request, *args, **kwargs)
    File &quot;C:\Users\RolfShin025\Desktop\E-COMMERCE\env\lib\site-packages\django\views\decorators\cache.py&quot;, line 57, in _wrapped_view_func
      response = view_func(request, *args, **kwargs)
    File &quot;C:\Users\RolfShin025\Desktop\E-COMMERCE\env\lib\site-packages\django\contrib\admin\sites.py&quot;, line 236, in inner
      return view(request, *args, **kwargs)
    File &quot;C:\Users\RolfShin025\Desktop\E-COMMERCE\env\lib\site-packages\django\contrib\admin\options.py&quot;, line 1673, in change_view
      return self.changeform_view(request, object_id, form_url, extra_context)
    File &quot;C:\Users\RolfShin025\Desktop\E-COMMERCE\env\lib\site-packages\django\utils\decorators.py&quot;, line 43, in _wrapper
      return bound_method(*args, **kwargs)
    File &quot;C:\Users\RolfShin025\Desktop\E-COMMERCE\env\lib\site-packages\django\utils\decorators.py&quot;, line 130, in _wrapped_view
      response = view_func(request, *args, **kwargs)
    File &quot;C:\Users\RolfShin025\Desktop\E-COMMERCE\env\lib\site-packages\django\contrib\admin\options.py&quot;, line 1549, in changeform_view
      return self._changeform_view(request, object_id, form_url, extra_context)
    File &quot;C:\Users\RolfShin025\Desktop\E-COMMERCE\env\lib\site-packages\django\contrib\admin\options.py&quot;, line 1645, in _changeform_view
      'subtitle': str(obj) if obj else None,
  TypeError: __str__ returned non-string (type NoneType)
  </code></pre>