Providing anonymous users the same function as registered users

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?

+6
python django session
source share
4 answers

I have not done this before, but reading your description, I would simply create a custom object when someone had to do something that he needed. Then you send a cookie to the user that refers to this user object, so if someone returns (without clearing their cookies), they get the same skeletal user object.

This means that you can use your current code with minimal changes, and when they want to switch to a fully registered user, you can just fill in the custom skeleton object with your details.

If you want to keep your tidy-ish DB, you can add a task that removes all skeletons. Users who have not been used in the last 30 days.

+9
source share

It seems to me that the easiest way to do this is to save both the user id and session id:

 class Cart(models.Model): user = models.ForeignKey(User, null=True) session = models.CharField(max_length=32, null=True) 

Then, when the user logs in, you can take their request.session.session_key and update all the lines with their new user ID.

Even better, you can define a UserProxy model:

 class Cart(models.Model): user = models.ForeignKey(UserProxy) class UserProxy(models.Model): user = models.ForeignKey(User, unique=True, null=True) session = models.CharField(max_length=32, null=True) 

So, you just need to update the UserProxy table when they register, and nothing about the cart should change.

+4
source share

Just save the user data in the user table and do not populate the userid / password tables.

if the user is registered, you just need to fill in these fields.

You will have to run a โ€œcleanupโ€ script periodically to clear all users who have not visited in any arbitrary period. I would make this cleanup optional. and have a script that can be run by the server (or through the web admin interface) to clear it if your client wants to do it manually.

Remember to delete all related entries as well as the user entry.

+2
source share

I think you were on the right track while thinking about using sessions. I would save the list of product identifiers in a user session, and then when the user logs in, create a basket as you defined, and then add the items. View the session documents .

You can allow people who are either not logged in or do not have an account to add items to the temp basket. When a user logs into any account or creates a new account, add these items to your "real" basket. Then, simply by adding a few lines to the โ€œadd product to cartโ€ and the login function, you can use existing models.

0
source share

All Articles