Frenggie
Frenggie

Reputation: 149

How can I organize a database with products and users?

I am currently trying to organize a django database model for an online shop-system with users and products.

My code:

class UserData(models.Model):
    username = models.CharField(max_length=100)
    password = models.CharField(max_length=500)
    bought_products = models.ForeignKey(MarketProducts, on_delete=models.CASCADE)

class VendorData(models.Model):
    username = models.CharField(max_length=100)
    password = models.CharField(max_length=500)
    sold_products = models.ForeignKey(MarketProducts, on_delete=models.CASCADE)

class MarketProducts(models.Model):
    category = models.CharField(max_length=100)
    vendor = models.ForeignKey(VendorData, on_delete=models.CASCADE)
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=1000)
    price = models.IntegerField()
    pub_date = models.DateTimeField('Date published')
    image = models.ImageField(upload_to=b'shop/media/images/')
    likes = models.IntegerField()
    dislikes = models.IntegerField()

How can I organize a good working system so all the products a user bought are saved inside the bought_products column and all the products a vendor sold can be saved inside the sold_products column. Do I have to use a ForeignKey for that or is there something more suitable for this situation? Also, if there is anything unwise about the existing structure of the database model (for example the current image field column only saves the link but not the image itself which is kinda weird...), please feel free to correct me :).

Many thanks in advance :D

Upvotes: 0

Views: 104

Answers (2)

brandonris1
brandonris1

Reputation: 455

Firstly I would start by reading the following documentation on django auth customization. Seems like that would help you out a little bit.

https://docs.djangoproject.com/en/2.2/topics/auth/customizing/

Additionally, I think you need to better evaluate your data modelling to make each model more explicit/clearly defined. See example below:

class Products(models.Model):
    vendor = models.ForeignKey(VendorData, on_delete=models.CASCADE)
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=1000)
    price = models.IntegerField()
    pub_date = models.DateTimeField('Date published')
    image = models.ImageField(upload_to=b'shop/media/images/')
    likes = models.IntegerField()
    dislikes = models.IntegerField()


class Category(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=100)
    active = models.BooleanField(default=True)


class ProductCategory(models.Model):
    product = models.ForeignKey(Products, on_delete=models.CASCADE)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    creation_date = models.DateTimeField(auto_add_now=True)

From there I would construct a separate model which would store the customer purchased items. Since you already have a model which stores the vendor to product data, you shouldn't need anything additional to identify how many sales a particular vendor has.

Upvotes: 0

Massimo Costa
Massimo Costa

Reputation: 1860

In this case I suggest to make bought_products and sold_products instances of ManyToManyField because the same product can be bought by multiple Users and sold by multiple vendors

Upvotes: 1

Related Questions