Mustapha Mohd Sagagi
Mustapha Mohd Sagagi

Reputation: 141

RelatedObjectDoesNotExist in Django

I am working on ecommerce site and I get this error:

"Exception Type: RelatedObjectDoesNotExist Exception Value: User has no customer."

models.py

class Customer(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE, primary_key=True)

views.py

def cart(request):
    customer=request.user.customer
    order,created=Order.objects.get_or_create(customer=customer,complete=False)
    items=order.orderitem_set.all()
    context={
        'items':items
    }

thanks beforehand.

Upvotes: 1

Views: 239

Answers (1)

Krishna Singhal
Krishna Singhal

Reputation: 661

You have to add related_name in OneToOneField

class Customer(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE, primary_key=True, related_name="customer')

Upvotes: 2

Related Questions