Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. Takie parametry mają używane tu pola:
  2.  id = models.AutoField(primary_key=True)
  3.  title = models.CharField(max_length=120)
  4.  slug = models.SlugField(unique=True, blank=True)
  5.  
  6.  def product_pre_save_receiver(sender, instance, *args, **kwargs):
  7.     last_id = sender.objects.all().last().id
  8.     instance.id = last_id + 1
  9.     new_slug = create_slug(instance)                            # 1
  10.     if not instance.slug or instance.slug != new_slug:    # 2
  11.        instance.slug = new_slug                                     # 3
  12.  
  13. tak wygląda create_slug
  14.  
  15.  def create_slug(instance, new_slug=None):
  16.     if new_slug:
  17.        slug = new_slug
  18.     else:
  19.         slug = slugify(instance.title)
  20.  
  21.     slug_exists = Product.objects.filter(slug=slug).exists()
  22.     if slug_exists:
  23.         new_slug = "%s-%s" % (slug, instance.id)
  24.         return create_slug(instance, new_slug=new_slug)
  25.     return slug
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement