So my question was how I can generate a random UID or slug for my CMS. If I use the default id which is coming from API v2 people can easily guess my next post URL easily.
Is there any way to add a unique slug/ID/UUID for my wagtail CMS?
So my question was how I can generate a random UID or slug for my CMS. If I use the default id which is coming from API v2 people can easily guess my next post URL easily.
Is there any way to add a unique slug/ID/UUID for my wagtail CMS?
A variant on the answer that I use for user ID's:
import random
import string
from django_extensions.db.fields import AutoSlugField
....
class CustomUser(AbstractUser):
....
uuid = AutoSlugField(unique=True)
....
def slugify_function(self, content):
return ''.join((random.choice(string.ascii_letters + string.digits) for i in range(12)))
Here is the simple solution, go to your
bolg/models.py
and first installpip install django-autoslug
Then import this
Here we are adding another extension called
get_random_string
which will generate a random string every time you call it.Then add this in your
AddStory
{Your add post class}Here I defined a function called
randomid
which will return a 10 digit string on every call. Then I created a new field called news_slug which is coming from Django auto_slug extension, wich will populate from the randomid, and the URL must unique (ex: if it all 10 digit string are finished it will add -1,-2 so on ( ex: sxcfsf12e4-1), herenull = true
means that this field can be empty so that autoslug can generate unique ID.Then expose that
news_slug
filed in API.you can access all field like this
/api/v2/pages/?type=blog.AddStory&fields=*
Here type=blog is your
blog
app andAddStory
is your class.Hope this helps, it took time for me to find out. More wagtail tutorials will come.