简体   繁体   English

调整当前代码以便能够将新表中的项目添加到购物篮

[英]Adjusting current code to be able to add items from new table to the basket

My website is currently adapted to deal with things like adding to the cart, removing from the cart and creating an order, only on one Item table.我的网站目前适用于处理诸如添加到购物车、从购物车中删除和创建订单之类的事情,仅在一个 Item 表上。 I wanted to add different products to the site, so I need to create a new table, that will handle other items.我想向站点添加不同的产品,因此我需要创建一个新表,用于处理其他项目。 The thing is, that I am not really sure how to properly implement that to my website, so things like adding to the cart and creating order would work with that new table as well.问题是,我不确定如何在我的网站上正确实施它,因此添加到购物车和创建订单之类的事情也适用于该新表。

So thats the model of the first Item table:所以这就是第一个 Item 表的模型:

class Item(models.Model):
  title = models.CharField(max_length=100)
  price = models.FloatField()
  discount_price = models.FloatField(blank=True, null=True)
  category = models.ManyToManyField(Category)
  label = models.CharField(choices=LABEL_CHOICES, max_length=1)
  slug = models.SlugField()
  description = models.TextField()
  image = models.ImageField()

  def __str__(self):
      return self.title

  def get_absolute_url(self):
      return reverse("core:product", kwargs={
          'slug': self.slug
      })

  def get_add_to_cart_url(self):
      return reverse("core:add-to-cart", kwargs={
          'slug': self.slug
      })

  def get_remove_from_cart_url(self):
      return reverse("core:remove-from-cart", kwargs={
          'slug': self.slug
      })

When you add item to the cart it goes to this table:当您将商品添加到购物车时,它会转到此表:

class OrderItem(models.Model):
  user = models.ForeignKey(settings.AUTH_USER_MODEL,
                         on_delete=models.CASCADE)
  ordered = models.BooleanField(default=False)
  item = models.ForeignKey(Item, on_delete=models.CASCADE)
  quantity = models.IntegerField(default=1)

Can I just add a new foreign key to this table and everything would work as it should?我可以向这个表添加一个新的外键,一切都会正常工作吗? How to deal with things like adding product from just one table?如何处理诸如仅从一张表添加产品之类的事情? The field for the other would be blank then?那么另一个字段会是空白的吗?

order model:订货型号:

class Order(models.Model):
  user = models.ForeignKey(settings.AUTH_USER_MODEL,
                         on_delete=models.CASCADE)
  ref_code = models.CharField(max_length=20, blank=True, null=True)
  items = models.ManyToManyField(OrderItem)
  start_date = models.DateTimeField(auto_now_add=True)
  ordered_date = models.DateTimeField()
  ordered = models.BooleanField(default=False)
  shipping_address = models.ForeignKey(
      'Address', related_name='shipping_address', on_delete=models.SET_NULL, blank=True, null=True)

and finally the new table, that will store other items:最后是新表,它将存储其他项目:

class Accessory(models.Model):
  title = models.CharField(max_length=100)
  price = models.FloatField()
  discount_price = models.FloatField(blank=True, null=True)
  category = models.ManyToManyField(Category)
  label = models.CharField(choices=LABEL_CHOICES, max_length=1)
  slug = models.SlugField()
  description = models.TextField()
  image = models.ImageField()

Anyone can help me get this to work with that new table called Accessory?任何人都可以帮助我将其与名为 Accessory 的新表一起使用吗?

Looking at your Item and Accessory model definitions, those two look very similar.查看您的ItemAccessory模型定义,这两个看起来非常相似。 My recommendation would be to create a OneToOne relationship between Accessory and Item tables, so you would not need to deal with this redundancy.我的建议是在AccessoryItem表之间创建OneToOne关系,这样您就不需要处理这种冗余。

If you do it like this, all the functionalities related to Item table would be inherited by the Accessory table as well.如果你这样做,所有与Item表相关的功能也将被Accessory表继承。 I think it will reduce costs and overheads of changing your logic.我认为这将减少更改逻辑的成本和开销。

class Accessory(models.Model):
    item = models.OneToOneField(Item, on_delete=models.CASCADE)
    ...
    # other fields that will differ between Accessory and Item

You can even use the connected Item record as your Accessory model's primary_key .您甚至可以使用连接的Item记录作为您的Accessory模型的primary_key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM