我的数据库中有3个表.User
,Flower
和Cart
.我正在尝试为用户登录的用户创建购物车体验,获取购物车,可以将商品添加到购物车,然后从购物车中订购商品.
我Cart
有一个ManyToManyField(Flower)
和我User
有一个OneToOneField(Cart)
.当用户向他们的购物车添加内容时,我需要为他们创建购物车,然后存储他们的用户ID以及他们点击添加到购物车的花ID(或名称).
目前这是我要经历的顺序......
1)通过数据库抓取用户 p1 = User.objects.filter(username = 'erik')
2)从列表中抓取用户对象 p1 = [0]
3) c1 = Cart(id = 1)
4) c1.user.add(p1)
我收到以下错误: RelatedObjectDoesNotExist: Cart has no user.
为什么这不起作用,我怎样才能让它做我需要它做的事情?
我的模特......
from __future__ import unicode_literals from django.contrib.auth.models import User from django.db import models class Flower(models.Model): name = models.CharField(max_length=255) link = models.CharField(max_length=255) created_at = models.DateField(auto_now_add=True) def __str__(self): return self.name class Meta: db_table = 'Flower' class Cart(models.Model): userid = models.CharField(max_length=255) flowername = models.CharField(max_length=255) user = models.OneToOneField(User) flower = models.ManyToManyField(Flower)
Aakash Rayat.. 5
我认为在这种情况下使用的正确架构如下:
class Cart(models.Model): user = models.OneToOneField(User, related_name='cart') date_added = models.DateTimeField(auto_now_add=True, help_text="Date when cart is added.") date_modified = models.DateTimeField(auto_now=True, help_text="Date when cart is modified.") class CartDetail(models.Model): cart = models.ForeignKey(Cart, related_name='cart_details') flower = models.ForeignKey(Flower) quantity = models.PositiveSmallIntegerField(help_text='Quantity to be purchased.') date_added = models.DateTimeField(auto_now_add=True, help_text="Date when this item is added.") date_modified = models.DateTimeField(auto_now=True, help_text="Date when this item is modified.")
因此,在这种情况下,您可以通过以下方式获取用户的购物车详情:
try: cart = request.user.cart except Cart.DoesNotExist: cart = Cart.objects.create(user=request.user) cart_details = cart.cart_details.all().order_by('-date_added')
在这里,如果用户刚刚登录,将为他创建一个新的购物车.
并且,您可以通过以下方式将项目添加到用户的购物车:
# Assuming you have flower object and quantity=number of qty. cart_item = CartDetail(flower=flower, cart=request.user.cart, quantity=quantity) cart_item.save()
这种方法比你正在尝试的方法更清洁.我希望这有帮助.
我认为在这种情况下使用的正确架构如下:
class Cart(models.Model): user = models.OneToOneField(User, related_name='cart') date_added = models.DateTimeField(auto_now_add=True, help_text="Date when cart is added.") date_modified = models.DateTimeField(auto_now=True, help_text="Date when cart is modified.") class CartDetail(models.Model): cart = models.ForeignKey(Cart, related_name='cart_details') flower = models.ForeignKey(Flower) quantity = models.PositiveSmallIntegerField(help_text='Quantity to be purchased.') date_added = models.DateTimeField(auto_now_add=True, help_text="Date when this item is added.") date_modified = models.DateTimeField(auto_now=True, help_text="Date when this item is modified.")
因此,在这种情况下,您可以通过以下方式获取用户的购物车详情:
try: cart = request.user.cart except Cart.DoesNotExist: cart = Cart.objects.create(user=request.user) cart_details = cart.cart_details.all().order_by('-date_added')
在这里,如果用户刚刚登录,将为他创建一个新的购物车.
并且,您可以通过以下方式将项目添加到用户的购物车:
# Assuming you have flower object and quantity=number of qty. cart_item = CartDetail(flower=flower, cart=request.user.cart, quantity=quantity) cart_item.save()
这种方法比你正在尝试的方法更清洁.我希望这有帮助.