demo 4

demo 4

class Meta: ordering = ['-created_at'] def save(self, *args, **kwargs): self.full_clean() # Validate fields before saving if not self.slug: self.slug = slugify(self.title) # Ensure slug uniqueness original_slug = self.slug count = 1 while Product.objects.filter(slug=self.slug).exists(): self.slug = f"{original_slug}-{count}" # Append -1, -2, etc. count += 1 super().save(*args, **kwargs) def __str__(self): return self.title # absolute url def clean(self): if not (self.doctor or self.clinic): raise ValidationError('You must select either a Doctor or a Clinic.') if self.doctor and self.clinic: raise ValidationError('You cannot select both a Doctor and a Clinic.') def get_absolute_url(self): return reverse('product_single', args=[ self.slug])

Leave a Comment

You must be Logged In to comment