问题
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?
- Simple database-based session.
- 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?
GuestCart
would live for exactly 1 week (count from theGuestCart
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.
回答1:
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.
来源:https://stackoverflow.com/questions/29113548/django-anonymous-session-temporary-one-to-one-related-model