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

空扰寡人 提交于 2020-05-13 06:09:46

问题


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.


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!