I would like to be able to change the slug using slugify if the slug already exist. This site will have multiple products named the same but when you call the product using get_object_or_404 you will get an error because two or more objects are being called at one time. To avoid this I need to auto increment the slugify if the slug already exist.
Can anyone help me with this?
class Product(models.Model):
product_name = models.CharField(max_length=500, blank=True)
product_description = models.TextField(blank=True)
company = models.ForeignKey(Company, blank=True, null=True)
category = models.ForeignKey(Category, blank=True, null=True)
manufacturer = models.ForeignKey(Manufacturer)
buy_link = models.URLField(max_length=1000, blank=True)
product_image_url = models.URLField(max_length=1000, blank=True)
price = models.CharField(max_length=30, blank=True)
orginal_price = models.CharField(max_length=30, blank=True)
stock = models.CharField(max_length=30, blank=True)
sku = models.CharField(max_length=250, blank=True)
slug = models.SlugField(max_length=500)
date_added = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.product_name)
super(Product, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('products:product_detail', args=[self.slug]) #kwargs={'slug': self.slug}
def __str__(self):
return self.product_name