Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Django: RelatedObjectDoesNotExist: Cart has no user

Tags:

python

django

I have 3 tables in my database. User, Flower, and Cart. I am trying create a shopping cart experience for a user where the user logs on, gets a shopping cart, can add things to their cart, and subsequently order things from their cart.

My Cart has a ManyToManyField(Flower) and my User has a OneToOneField(Cart). When the user adds something to their cart I need to create a cart for them, and then store their user id and the flower id (or name) that they clicked to add to their cart.

Currently this is the sequence I am going through...

1) Grab the user from the database via p1 = User.objects.filter(username = 'erik')

2) Grab the user object out of the list p1 = [0]

3) c1 = Cart(id = 1)

4) c1.user.add(p1)

I am getting the following error: RelatedObjectDoesNotExist: Cart has no user.

Why is this not working and how can I get it to do what I need it to do?

My Model...

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

class Flower(models.Model):
    name = models.CharField(max_length=255)
    link = models.CharField(max_length=255)
    created_at = models.DateField(auto_now_add=True)

    def __str__(self):
        return self.name
class Meta:
    db_table = 'Flower'

class Cart(models.Model): 
    userid = models.CharField(max_length=255)
    flowername = models.CharField(max_length=255) 
    user = models.OneToOneField(User)
    flower = models.ManyToManyField(Flower)
like image 859
Erik Åsland Avatar asked Nov 17 '25 05:11

Erik Åsland


1 Answers

I think the correct architecture in this case to use is below:

class Cart(models.Model):
    user = models.OneToOneField(User, related_name='cart')        
    date_added = models.DateTimeField(auto_now_add=True, help_text="Date when cart is added.")
    date_modified = models.DateTimeField(auto_now=True, help_text="Date when cart is modified.")


class CartDetail(models.Model):
    cart = models.ForeignKey(Cart, related_name='cart_details')
    flower = models.ForeignKey(Flower)
    quantity = models.PositiveSmallIntegerField(help_text='Quantity to be purchased.')
    date_added = models.DateTimeField(auto_now_add=True,
                                      help_text="Date when this item is added.")
    date_modified = models.DateTimeField(auto_now=True, help_text="Date when this item is modified.")

So in this case, You can get a user's cart details by:

try:
    cart = request.user.cart
except Cart.DoesNotExist:
    cart = Cart.objects.create(user=request.user)
    cart_details = cart.cart_details.all().order_by('-date_added')

Here, if user is just logged in, a new cart will be created for him.

And, you can add an item to user's cart by this:

# Assuming you have flower object and quantity=number of qty.
cart_item = CartDetail(flower=flower, cart=request.user.cart, quantity=quantity)

cart_item.save()

This approach is much more cleaner than the one you are trying. I hope this helps.

like image 157
Aakash Rayate Avatar answered Nov 18 '25 19:11

Aakash Rayate



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!