django-utils v0.3.0 documentation
This library includes several subclasses of django model fields.
Field to store status of model instance, i.e. “LIVE”, “DRAFT”, “DELETED”. Used by the djutils.db.managers.PublishedManager to expose the published() method.
Example:
from django.db import models
from djutils.db.fields import StatusField
from djutils.db.managers import PublishedManager
class BlogEntry(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
status = StatusField()
objects = PublishedManager('status') # use the status field
To return only those blog entries whose status is LIVE, we can query the manager:
>>> published_blogs = BlogEntry.objects.published()
Field that generates unique slugs in the event of collisions, optionally accepting a date-field.
It attempts to automate several of the pain-points with SlugFields:
- collisions:
if a slug is specified as unique, a collision can cause you to get IntegrityErrors in your database – if you’re using your slug fields with a date_field (i.e. /news/2010/mar/5/some-headline/) then you will want to validate uniqueness for that day only.
- autopopulation:
a slug is generally a URL-friendly representation of another field, such as a headline or title
- truncation:
slugs can be truncated in such a way as to not break up a word, so instead of “some-interesting-headli” you’d get “some-interesting”
- underscores or numbers:
if a collision is encountered, you can either append underscores or use a number:
underscores: "slug", "slug_", "slug__", "slug___" numbers: "slug", "slug-1", "slug-2", "slug-3"
Parameters: |
|
---|