automatically generate random post slug

This commit is contained in:
Federico Justus Denkena 2023-11-06 17:34:45 +01:00
parent ef4511e512
commit 3cc72a6d48
Signed by: f-denkena
GPG Key ID: 28F91C66EE36F382
4 changed files with 21 additions and 1 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 4.2.1 on 2023-11-06 16:34
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('website', '0013_alter_post_overview'),
]
operations = [
migrations.AlterField(
model_name='post',
name='slug',
field=models.SlugField(blank=True, editable=False),
),
]

View File

@ -3,6 +3,7 @@ from django.db import models
from django.forms import ModelForm from django.forms import ModelForm
from django import forms from django import forms
from captcha.fields import CaptchaField from captcha.fields import CaptchaField
from random import randrange
# Create your models here. # Create your models here.
User = get_user_model() User = get_user_model()
@ -22,7 +23,7 @@ class Category(models.Model):
class Post(models.Model): class Post(models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE) author = models.ForeignKey(Author, on_delete=models.CASCADE)
slug = models.SlugField() slug = models.SlugField(blank=True, editable=False)
overview = models.TextField(blank=True, editable=False) overview = models.TextField(blank=True, editable=False)
timestamp = models.DateTimeField(auto_now_add=True) timestamp = models.DateTimeField(auto_now_add=True)
content = models.TextField() content = models.TextField()
@ -34,6 +35,7 @@ class Post(models.Model):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.overview = self.content[:33] + "..." self.overview = self.content[:33] + "..."
if not self.slug: self.slug = self.title[:7].lower() + "-" + str(randrange(1000, 9999, 1))
super().save(*args, **kwargs) super().save(*args, **kwargs)
class Contact(models.Model): class Contact(models.Model):