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?
GuestCart
model when they need:
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?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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With