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. 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:
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?
Looking at your Item
and Accessory
model definitions, those two look very similar. My recommendation would be to create a OneToOne
relationship between Accessory
and Item
tables, so you would not need to deal with this redundancy.
If you do it like this, all the functionalities related to Item
table would be inherited by the Accessory
table as well. 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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.