def clean(self): ''' Provide some custom validation and other tasks. ''' # Set pub_date if none exist and publish is true. if self.status == PUBLISHED_STATUS and not self.pub_date: self.pub_date = datetime.now() # No publishing without a pub_date # Create slug if none exists and it's published. if not self.slug and self.status == PUBLISHED_STATUS: qs = Post.objects.published().filter(pub_date__startswith=self.pub_date.date()) # Slug should be unique for date. unique_slugify(self, self.title, queryset=qs) # Validate unique slug by pub_date. Unique_for_date only validates at # the model form level. if self.slug: p = Post.objects.exclude(pk=self.pk).filter(slug=self.slug) if self.pub_date: p = p.filter(pub_date__startswith=self.pub_date.date()) else: p = p.filter(pub_date__isnull=True) # if slug isn't None and a match is found on that date, throw error. if p: raise ValidationError("Slug must be unique for author and pub_date")
def save(self, force_insert=False, force_update=False): '''Custom save method performs some needed juggling of the object.''' # Make sure cached info is synced with current state of the object. if self.pk in SITE_CACHE: del SITE_CACHE[self.pk] if self.default: # Make sure there is only one default site information. SiteInformation.objects.all().update(default=False) if not self.slug: # Autogenerate a slug if one isn't provided. unique_slugify(self, self.title) super(SiteInformation, self).save(force_insert, force_update)
def forwards(self, orm): # Changing field 'Post.slug' # first populate field. for post in Post.objects.raw().filter(models.Q(slug__isnull=True) | models.Q(slug='')): post.slug = unique_slugify(post, post.title, queryset=Post.objects.filter(pub_date__startswith=post.pub_date.date)) post.save() db.alter_column('blog_post', 'slug', self.gf('django.db.models.fields.SlugField')(default='', max_length=75))
def save(self, force_insert=False, force_update=False): ''' Custom save method to handle slugs and pubdate such. ''' # Set pub_date if none exist and publish is true. if not self.pub_date: self.pub_date = datetime.now() # No publishing without a pub_date if not self.slug: qs = Post.objects.raw().filter(pub_date__year=self.pub_date.year, pub_date__month=self.pub_date.month, pub_date__day=self.pub_date.day) # Slug should be unique for date. unique_slugify(self, self.title, queryset=qs) super(Post, self).save(force_insert, force_update) # Actual Save method.
def save(self, force_insert=False, force_update=False): ''' Custom save method to handle slugs and pubdate such. ''' # Set pub_date if none exist and publish is true. if not self.pub_date: self.pub_date = datetime.now() # No publishing without a pub_date if not self.slug: qs = Post.objects.raw().filter( pub_date__year=self.pub_date.year, pub_date__month=self.pub_date.month, pub_date__day=self.pub_date.day ) # Slug should be unique for date. unique_slugify(self, self.title, queryset=qs) super(Post, self).save(force_insert, force_update) # Actual Save method.