Advertisement
horozov86

Save method in Django overwriting, example

Aug 29th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. from django.db import models
  2. from django.utils.text import slugify
  3.  
  4. class Article(models.Model):
  5.     title = models.CharField(max_length=255)
  6.     content = models.TextField()
  7.     slug = models.SlugField(unique=True, blank=True)
  8.  
  9.     def save(self, *args, **kwargs):
  10.         if not self.slug:
  11.             self.slug = slugify(self.title)
  12.         super().save(*args, **kwargs)
  13.  
  14. # Създаване на нова статия
  15. article = Article(title="Django Tips", content="Some useful tips for Django developers.")
  16. article.save()  # Slug ще бъде генериран автоматично, ако не е предоставен
  17.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement