I have these models in my models.py:
class Category(models.Model):
main_category= models.ForeignKey(HovedKategori, on_delete=models.CASCADE)
name= models.CharField(max_length=200)
def __str__(self):
return self.name
class UnderCategori(models.Model):
main_kategory= models.ForeignKey(Category, on_delete=models.CASCADE)
supplier = models.CharField(max_length=200)
price = models.IntegerField()
title = models.CharField(max_length=200)
img = models.CharField(max_length=200)
info= models.CharField(max_length=200)
link= models.CharField(max_length=200)
def __str__(self):
return str(self.supplier ) + " " + str(self.title)
Ho do i query all the different category suppliers without repeating them? What i mean is if i have 10 different suppliers with 500 products combined how do i query the 10 different suppliers. Also im not sure if this is the correct data stucture i should use, if there are any tips on that, that would also be appretiated.
I would suggest splitting the supplier from the category like so:
class Supplier(models.Model):
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class Category(models.Model):
main_category = models.ForeignKey(HovedKategori, on_delete=models.CASCADE)
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class UnderCategori(models.Model):
main_kategory= models.ForeignKey(Category, on_delete=models.CASCADE)
supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
price = models.IntegerField()
title = models.CharField(max_length=200)
img = models.CharField(max_length=200)
info= models.CharField(max_length=200)
link= models.CharField(max_length=200)
def __str__(self):
return str(self.supplier ) + " " + str(self.title)
Then you could simply do Supplier.objects.all()
Please note that I don't really understand why there are two different categories. Perhaps one is a sub-category? Also, you are talking about products, not categories. I don't see any 'products' mentioned in your code
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.