Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Anonymous session & "temporary" one-to-one related model?

Currently, I have a Cart model for each registered CustomUser (OneToOne relation):

class Cart(models.Model):
    created_at = models.DateTimeField(auto_now_add = True)
    user = models.OneToOneField(CustomUser, related_name='cart')

But now I have some requirement for anonymous user (guest). after looking for some articles, session seems to be required?

  1. Simple database-based session.
  2. How to let guest has a related temporary GuestCart model when they need:
    • Because I don't expect even a robot has a cart to waste resource.
    • So...may I GuestCart.objects.create(session = request.session['cart']) #just pseudo-code, I don't sure how to do this when a view is called by the guest?
  3. GuestCart would live for exactly 1 week (count from the GuestCart is established. Time won't updated for guest's activity), and should be deleted automatically when expired.

** Update @ 2015/3/21: currently, I add a new key cart_id into request.session, and not save session info into DB.

like image 318
kuanyui Avatar asked Feb 10 '23 15:02

kuanyui


1 Answers

In order to support anonymous users i believe your Cart model class should looks like this;

class Cart(models.Model):
    created_at = models.DateTimeField(auto_now_add = True)
    user = models.OneToOneField(CustomUser, blank=True, null=True, related_name='cart')
    session_key = models.CharField(max_length=40)

  class Meta:
    unique_together = ('user', 'session_key',)

Then you can retrieve the user cart with something like this:

if request.user.is_authenticated():
    cart, created = Cart.objects.update_or_create(
        user = request.user,
        defaults = {'session_key': request.session.session_key}
    )
else:
    cart, created = Cart.objects.get_or_create(
        session_key = request.session.session_key,
        defaults = {'user': None}
    )

and later on if the checkout process required registration you can always alter the cart and add the user.

like image 118
Todor Avatar answered Feb 13 '23 21:02

Todor