1

Lately I've been needing to create a server-side shopping cart for my website that can have products inside. I could add cart and product in session cookies, but I prefer to add it to my custom made User model, so it can stay when the user decides to log out and log back in.

Since I'm making a shopping cart, I need to make a model for it, that will hold products as objects, so then they can be easily used, but I can't find any way to do so properly.

class User(models.Model):
    username = models.CharField(max_length=150)
    joined = models.DateTimeField('registered')
    avatar = models.CharField(max_length=300)
    balance = models.IntegerField(default=0)
    ban = models.BooleanField()
    cart = models.???


    def __str__(self):
        return self.username

How can I achieve having array string in models with using Django's system? If not then can it be possible by other libraries ( Json, Pickle ), but I've seen it can be done by ForeignKey, if so how is it possible?

Diego Allen
  • 4,623
  • 5
  • 30
  • 33
ShellRox
  • 2,532
  • 6
  • 42
  • 90
  • http://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad/3653574 – e4c5 Oct 09 '16 at 15:53

1 Answers1

2

I'd suggest using DB relationships instead of storing a string or an array of strings for this problem.

If you solve the problem using DB relationships, you'll need to use a ManyToManyField.

class User(models.Model):
    ...
    cart = models.ManyToManyField(Product)

And assuming you have a model for your products.

class Product(models.Model):
    name = models.CharField(max_length=30)
    price = models.DecimalField(max_digits=8, decimal_places=2)

When adding elements to the cart you'll use something like this.

tv = Product(name='LED TV', ...)
tv.save()
diego = User.objects.get(username='dalleng')
diego.cart.add(some_product)
Diego Allen
  • 4,623
  • 5
  • 30
  • 33
  • Thanks, also what do i need to put in shells? Because when i try `Product` it gives error : `NameError: name 'Product' is not defined` – ShellRox Oct 09 '16 at 15:20
  • 1
    I'm assuming you already created a Product Model. When you're using the Django shell, you need to import all models. – Diego Allen Oct 09 '16 at 15:23
  • I am sorry for not understanding, what do i need to put in `cart = models.ManyToManyField(Product)` so i can replace product?, it takes exactly 2 arguments, if i give nothing it gives me that TypeError, if not it will give NameError. – ShellRox Oct 09 '16 at 15:38
  • You have to put the model class you're looking to make a relationship with. In your case you should have a Product model. – Diego Allen Oct 09 '16 at 15:43
  • 1
    Using DB relationships and using arrays of strings are two different things. – Diego Allen Oct 09 '16 at 15:50
  • Thanks for responding again, what do i need to put to replace `(some_product)`, sorry it's bit complicated... should it be product object? – ShellRox Oct 09 '16 at 16:02
  • @ShellRox some_product should be replaced by an instance of a product model class. Updated the answer to make it clearer. – Diego Allen Oct 09 '16 at 16:06
  • Also, last thing i need to ask, how can i use cart details now? when i try `user.cart()` it gives me this error: `manager = getattr(self.model, kwargs.pop('manager'))` @Diego Allen – ShellRox Oct 09 '16 at 16:15