Answer the question
In order to leave comments, you need to log in
How to make slug repeat title?
Please tell me how to make it so that when filling in the title field, the slug is filled in by itself
class Book(models.Model):
title = models.CharField(max_length=155, verbose_name='Название книги')
summary = models.TextField(blank=True, verbose_name='Краткое описание')
photo = models.ImageField(upload_to='books/%m/%d', null=True, blank=True)
genre = models.ForeignKey(Genre, on_delete=models.CASCADE, null=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE, null=True)
creation_date = models.DateField(blank=True, verbose_name='Дата создания')
slag = models.SlugField(unique=True)
def __str__(self):
return self.title
Answer the question
In order to leave comments, you need to log in
from django.utils.text import slugify
class Book(models.Model):
...
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Book, self).save(*args, **kwargs)
class BooksAdmin(admin.ModelAdmin):
...
prepopulated_fields = {'slug': ('title',)}
...
admin.site.register(Book, BooksAdmin)
The answer is found quite simply, from third-party libraries to a simple solution while saving
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question