Customizing Slug Field for Post Model | Django CMS Building By shriekdj
Creating Custom CMS In Django | Django CMS Building By shriekdj
Table of contents
So In Previous Post I Created The Simple Django Model of the BlogPost
Which Stores Data Like Given Below.
class Post(models.Model):
title = models.CharField(verbose_name='title', max_length=255, null=False)
content = models.TextField(verbose_name='content', null=False, blank=True)
created_on = models.DateTimeField(auto_now=True, auto_created=True, null=False, blank=False)
published_on = models.DateTimeField(null=True)
updated_on = models.DateTimeField(null=True)
But It does Not have any slug field so I am Adding them it will be like given below
from typing import Iterable, Optional
from django.db import models
# Create your models here.
class Post(models.Model):
title = models.CharField(verbose_name='title', max_length=255, null=False)
content = models.TextField(verbose_name='content', null=False, blank=True)
created_on = models.DateTimeField(auto_now=True, auto_created=True, null=False, blank=False)
published_on = models.DateTimeField(null=True)
updated_on = models.DateTimeField(null=True)
slug = models.SlugField(verbose_name='slug', max_length=255, unique=True)
def save(self, force_insert: bool = ..., force_update: bool = ..., using: Optional[str] = ..., update_fields: Optional[Iterable[str]] = ...) -> None:
return super().save(force_insert, force_update, using, update_fields)
Here there is also a new method is added here. don't worry you don't have to remember it as it is just type def save
under the model in your code editor and it auto fill the save()
method of models.Model
Base Class Also from typing import Iterable, Optional
for working with optional values.
There is Function Named slugify()
in Built-in module of django django.utils.text
just import it and modify the last one save() method like given below.
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super(Post, self).save(*args, **kwargs)
Now This may make our slug easily but it will have issues like we can't create Posts with same title so we will add new function here.
import random
from typing import Iterable, Optional
from django.db import models
from django.utils.text import slugify
def random_words():
return ''.join(random.choice('abcdefghijklmnopqrstuvwxyz0123456789'.split('')) for _ in range(6))
# Create your models here.
class Post(models.Model):
title = models.CharField(verbose_name='title', max_length=255, null=False)
content = models.TextField(verbose_name='content', null=False, blank=True)
created_on = models.DateTimeField(auto_now=True, auto_created=True, null=False, blank=False)
published_on = models.DateTimeField(null=True)
updated_on = models.DateTimeField(null=True)
slug = models.SlugField(verbose_name='slug', max_length=255, unique=True)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title + random_words())
super(Post, self).save(*args, **kwargs)
here we added random module and random_words()
functions for auto creating slug if not informed.
Also register that model in admin.py
of the blog
app folder like given below
from django.contrib import admin
# Register your models here.
from .models import Post
admin.site.register(Post)
Resultant Creation Form Will Look Like Given Below After migrating the Database.
Link to the Repo
Conclusion
If you found this useful then please share this and follow me! Also check out Buy Me A Coffee if you want to support me on a new level!