1
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')
Justin
  • 1,329
  • 3
  • 16
  • 25

1 Answers1

1

referring to django docs which states the following: Field.unique_for_date¶ Set this to the name of a DateField or DateTimeField to require that this field be unique for the value of the date field.

For example, if you have a field title that has unique_for_date="pub_date", then Django wouldn’t allow the entry of two records with the same title and pub_date.

Note that if you set this to point to a DateTimeField, only the date portion of the field will be considered. Besides, when USE_TZ is True, the check will be performed in the current time zone at the time the object gets saved.

This is enforced by Model.validate_unique() during model validation but not at the database level. If any unique_for_date constraint involves fields that are not part of a ModelForm (for example, if one of the fields is listed in exclude or has editable=False), Model.validate_unique() will skip validation for that particular constraint.

https://docs.djangoproject.com/en/3.0/ref/models/fields/#unique-for-date

this you can consider as unique_together which in your case you cant save same post with the same slug in same day

example: if you at 3/6/2020 and your slug is Hello if you try to write same slug with name Hello again in same day it will be refused but if you write the same slug in other day it will be accepted

Mohamed Beltagy
  • 593
  • 4
  • 8