I don't know if these models are setup correctly for django ecommerce

` from django.db import models

from phonenumber_field.modelfields import PhoneNumberField

Create your models here.

CATGORIES_CHOICE = ( ("Sports"), ("Fashion"), ("Electronics"), ("Fishing"), ("Books"), ("Cosmetics"), ("Games"), ("Pets"), ("Outwear"), ("Lingerie"), ("Medicals") )

ORDER_HISTORY_CHOICE = ()

class User(models.Model):

email = models.EmailField(unique=True)

password = models.CharField(max_length=15)

phone_number = PhoneNumberField(region="US")

address1 = models.TextField()

address2 = models.TextField(blank=True,null=True)

created = models.DateTimeField(auto_now_add=True) 

class Store(models.Model):

user = models.ForeignKey(User,on_delete=models.CASCADE)

name = models.CharField(max_length=100,default="official store")

created = models.DateTimeField(auto_now_add=True)
    

class Product(models.Model):

store_id = models.ForeignKey(Store,on_delete=models.CASCADE)

#product_id = models.IntegerField(unique=True,max_length=13)

title = models.CharField(max_length=110)

description = models.TextField(null=True, blank=True)

price = models.DecimalField(max_digits=15,decimal_places=2)

discount = models.DecimalField(max_digits=2,decimal_places=2,default=0.00)

available = models.IntegerField()

category = models.CharField(choices="",max_length=20)

# image = models.ImageField(upload_to="images")

condition = models.CharField(choices="",max_length=20)

created = models.DateTimeField(auto_now_add=True)

updated = models.DateTimeField(auto_now=True)

@property
def sales_price(self):
    return "%f" %(float(self.price * (1 - self.discount)))

class CartItem(models.Model):

item = models.ForeignKey(Product,on_delete=models.CASCADE)

quantity = models.PositiveIntegerField(default=1)

class Cart(models.Model):

user = models.ForeignKey(User,on_delete=models.CASCADE)

order_number = models.IntegerField(unique=True,default=7)

ordered = models.BooleanField(default=False)

items = models.ManyToManyField(CartItem)

created_date = models.DateTimeField(auto_now_add=True)

ordered_date = models.DateTimeField()

class Review(models.Model):

created_by = models.ForeignKey(User,on_delete=models.CASCADE)

product_id = models.ManyToManyField(Product,on_delete=models.CASCADE)

rating = models.DecimalField(max_digits=2,decimal_places=1,default=0.0)

label = models.CharField(max_length=100)

comment = models.TextField(max_length=500)

created = models.DateTimeField(auto_now_add=True)

updated = models.DateTimeField(auto_now=True)

class BlogPostInfo(models.Model):

title = models.CharField(max_length=100)

written_by = models.CharField(max_length=70)

created = models.DateTimeField(auto_now_add=True)

updated = models.DateTimeField(auto_now=True)

class Saved(models.Model):

user = models.ForeignKey(User,on_delete=models.CASCADE)

products = models.ManyToManyField(Product)

class History(models.Model):

user = models.ForeignKey(User,on_delete=models.CASCADE)

products = models.ManyToManyField(Product)

status = models.CharField(choices="",max_length=20) `

change your category field in Product:

category = models.ForeignKey("Category", on_delete=models.CASCADE, null=True, blank=True)

and create a Category Class:

class Category(models.Model):
    name = models.CharField(max_length=128)

    def __str__(self):
        return str(self.name) or ""

this will allow you to do add any category and it will be on a different table linked by the foreignkey

Back to Top