Tajs
Tajs

Reputation: 621

Store product URL for each retailer and product with Django models

I'm trying to create a database storing information for various products and retailers. I'm trying to capture information about all URLs that certain products can be found on retailers websites. In essence, think of a traditional price checker website. There is a product page that shows all the URLs of the product stocked by various retailers.

It's probably a rather easy solution but I'm having issues wrapping my head around it.

I'm not sure where should I capture the URL of certain product for each retailer.

I believe URLs should be stored in the Product model which would require some kind of dictionary to hold information about retailer and product URL sold by those retailers.

class Manufacturer(models.Model):
    manufacturers = models.CharField(max_length=10,choices=manufacturer_list,default='30')
    country = models.CharField(max_length=10,choices=country_list,default='30')


class Retailers(models.Model):
    retailer_name = models.CharField(max_length=10,choices=retailer_list,default='30')
    retailer_website = models.URLField(max_length=250)
    retailer_page = models.URLField(max_length=250)
    country = models.CharField(max_length=255)


class Product(models.Model):
    name = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=4,decimal_places=2)
    product_image_url = models.URLField(max_length=250)
    product_manufacturer_url = models.URLField(max_length=250)
    product_size = models.CharField(max_length=10,choices=bottle_sizes,default='30')
    manufacturer = models.ForeignKey(Manufacturer,on_delete=models.CASCADE)
    reatailers = models.ManyToManyField(Retailers)

Hope that makes sense, Mat

Upvotes: 0

Views: 131

Answers (1)

ivant
ivant

Reputation: 26

You can add extra fields to the intermediate model of a many-to-many relationship.

class Product(models.Model):
    ...
    reatailers = models.ManyToManyField(Retailers, through='ProductRetailer')

class ProductRetailer(models.Model):
    url = models.CharField(max_length=255)
    product = models.ForeignKey(Product)
    retailer = models.ForeignKey(Retailers)

see documentation

Upvotes: 1

Related Questions