at present, I have obtained the product details through the crawler. I want to submit and save the product information to my item table
through post, and put it into the shopping cart
while saving the data. The product information in the shopping cart is obtained by associating with the item table. How should it be realized?
beginners ask for advice. no, no, no.
-sharp
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
-sharp 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")
-sharp
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")