I am working on an online store in Django (now just a basic shopping cart), and I plan to add functionality for users to mark items as favorites (as in stackoverflow). Models for the basket look something like this:
class Cart(models.Model): user = models.OneToOneField(User) class CartItem(models.Model): cart = models.ForeignKey(Cart) product = models.ForeignKey(Product, verbose_name="produs")
The selected model will be just a table with two rows: the user and the product.
The problem is that this will only work for registered users, since I need a custom object. How can I allow unregistered users to use these functions by storing data in cookies / sessions, and when and if they decide to register by moving the data to their user?
I think one option would be a kind of generic relationship, but I think it's a little complicated. Perhaps you have an extra line after the user that the session object (I still have not used sessions in django), and if the user is set to None, use this?
So basically, I want to ask if you have this problem before, how did you solve it, what would be the best approach?
python django session
Adrian mester
source share