First, I am reading a book on django and from it have created the following models.py (entire models.py included at the end, the two fields related to the question are here):
slug = models.SlugField(max_length = 250,
unique_for_date = 'publish') #unique_for_date ensures that there will be only one post with a slug for a given date, thus we can retrieve single posts using date and slug
publish = models.DateTimeField(default = timezone.now)
Anyway, I have been searching online and have seen that you can use the unique_for_date
parameter when creating a DateTimeField()
but when it comes to using the parameter in a SlugField()
I found one question asked on here almost ten years ago (How to create a unique_for_field slug in Django?) and so figured it wouldn't hurt to ask. If this can be done, can someone explain what that parameter is doing? Is this maybe something that is now obsolete in django? Thanks in advance.
models.py:
# Create your models here.
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Pusblished'),
)
title = models.CharField(max_length = 250)
slug = models.SlugField(max_length = 250,
unique_for_date = 'publish') #unique_for_date ensures that there will be only one post with a slug for a given date, thus we can retrieve single posts using date and slug
author = models.ForeignKey(User,
on_delete= models.CASCADE,
related_name= 'blog_posts'
)
body = models.TextField()
publish = models.DateTimeField(default = timezone.now)
created = models.DateTimeField(auto_now_add = True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length = 10,
choices = STATUS_CHOICES,
default = 'draft')