目前我通过爬虫获取到了商品详情信息,想通过 post 提交保存到我的商品表
,并且在保存数据的同时将其放入到购物车
里,购物车里的商品信息是通过与商品表关联获取。应该怎么实现呢?
新手求指点。。
# 购物车模型
from django.db import models
from goods.models import Goods
class Cart(models.Model):
cart_id = models.CharField(max_length=250, blank=True, verbose_name="购物车 id")
add_time = models.DateField(auto_now_add=True, verbose_name="创建时间")
class Meta:
db_table = "Cart"
ordering = ['add_time']
verbose_name ='购物车'
verbose_name_plural = verbose_name
def __str__(self):
return self.cart_id
class CartItem(models.Model):
goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品名")
cart = models.ForeignKey(Cart, on_delete=models.CASCADE, verbose_name="购物车 id")
quantity = models.IntegerField(verbose_name="商品数量")
active = models.BooleanField(default=True)
class Meta:
db_table = "CartItem"
verbose_name = "购物车商品"
verbose_name_plural = verbose_name
def sub_total(self):
return self.goods.good_price * self.quantity
def __str__(self):
return self.goods
# from 提交保存到商品表
def post(self, request):
user = UserProfile.objects.get(username=request.user)
new_goods = Goods(goods_user=user)
goods_from = goodsForm(request.POST, instance=new_goods)
if goods_from.is_valid():
goods_from.save()
return redirect('cart:cart_detail')
else:
goods_from =goodsForm()
return redirect('index')
# 添加到购物车
def add_cart(request, goods_id):
goods = Goods.object.get(id=goods_id)
try:
cart = Cart.object.get(cart_id=_cart_id)
except Cart.DoseNotExist:
cart = Cart.object.create(
cart_id = _cart_id(request)
)
cart.save(),
try:
cart_item = CartItem.object.get(goods=goods, cart=cart)
cart_item.quantity +=1
cart_item.save()
except CartItem.DoseNotExist:
cart_item = CartItem.object.create(
goods = goods,
quantity = 1,
cart = cart
)
cart_item.save()
return redirect('cart:cart_detail')
@virusdefender
''' def post(self, request): user = UserProfile.objects.get(username=request.user) new_goods = Goods(goods_user=user) goods_from = DaikouForm(request.POST, instance=new_goods) if goods_from.is_valid(): goods_from.save() goods = Goods.object.get(id=new_goods.id) try: cart = Cart.object.get(cart_id=_cart_id) except Cart.DoseNotExist: cart = Cart.object.create( cart_id = _cart_id(request) ) cart.save(), try: cart_item = CartItem.object.get(goods=goods, cart=cart) cart_item.quantity += 1 cart_item.save() except CartItem.DoseNotExist: cart_item = CartItem.object.create( goods = goods, quantity = 1, cart = cart ) cart_item.save() return redirect('cart:cart_detail') else: goods_from =DaikouForm() return redirect('index') '''
def post(self, request):
user = UserProfile.objects.get(username=request.user)
new_goods = Goods(goods_user=user)
goods_from = DaikouForm(request.POST, instance=new_goods)
if goods_from.is_valid():
goods_from.save()
goods = Goods.object.get(id=new_goods.id)
try:
cart = Cart.object.get(cart_id=_cart_id)
except Cart.DoseNotExist:
cart = Cart.object.create(
cart_id = _cart_id(request)
)
cart.save(),
try:
cart_item = CartItem.object.get(goods=goods, cart=cart)
cart_item.quantity += 1
cart_item.save()
except CartItem.DoseNotExist:
cart_item = CartItem.object.create(
goods = goods,
quantity = 1,
cart = cart
)
cart_item.save()
return redirect('cart:cart_detail')
else:
goods_from =DaikouForm()
return redirect('index')
1
Kilerd 2018-05-10 23:05:15 +08:00
抽象出 Service 层
|
3
virusdefender 2018-05-11 00:02:18 +08:00
没懂你是什么问题,合并一下两个函数的代码不就可以了么
|
4
myung OP @virusdefender。。我合并了 但是没反应。。
|
5
myung OP @virusdefender 合并 post 提交 俩函数都不起作用了。。。
|
6
virusdefender 2018-05-11 00:10:15 +08:00
@myung #5 那就是你写错了,贴一下代码看看
|
7
myung OP @virusdefender
''' def post(self, request): user = UserProfile.objects.get(username=request.user) new_goods = Goods(goods_user=user) goods_from = DaikouForm(request.POST, instance=new_goods) if goods_from.is_valid(): goods_from.save() goods = Goods.object.get(id=new_goods.id) try: cart = Cart.object.get(cart_id=_cart_id) except Cart.DoseNotExist: cart = Cart.object.create( cart_id = _cart_id(request) ) cart.save(), try: cart_item = CartItem.object.get(goods=goods, cart=cart) cart_item.quantity += 1 cart_item.save() except CartItem.DoseNotExist: cart_item = CartItem.object.create( goods = goods, quantity = 1, cart = cart ) cart_item.save() return redirect('cart:cart_detail') else: goods_from =DaikouForm() return redirect('index') ''' |
8
myung OP @virusdefender 就是直接把 添加到购物那个函数内容 贴到保存数据库的后面了
|
9
myung OP ```
def post(self, request): user = UserProfile.objects.get(username=request.user) new_goods = Goods(goods_user=user) goods_from = DaikouForm(request.POST, instance=new_goods) if goods_from.is_valid(): goods_from.save() goods = Goods.object.get(id=new_goods.id) try: cart = Cart.object.get(cart_id=_cart_id) except Cart.DoseNotExist: cart = Cart.object.create( cart_id = _cart_id(request) ) cart.save(), try: cart_item = CartItem.object.get(goods=goods, cart=cart) cart_item.quantity += 1 cart_item.save() except CartItem.DoseNotExist: cart_item = CartItem.object.create( goods = goods, quantity = 1, cart = cart ) cart_item.save() return redirect('cart:cart_detail') else: goods_from =DaikouForm() return redirect('index') ``` |
10
myung OP @virusdefender 贴到第 2 个附言了。。。贴错 3 次。
|
11
WordTian 2018-05-11 00:45:02 +08:00 via Android
python 不太熟,反正代码没看出问题,你发 post 的 url 接口是对应你改完的方法吗
|