github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

jaylett / django_auto_sluggable

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 3
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Django abstract model that on create will generate a slug unique across a configurable QuerySet — Read more

  cancel

http://tartarus.org/james/computers/django/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

slug_objects should be an instance method, not a class method. 
jaylett (author)
Mon Sep 14 12:11:27 -0700 2009
commit  0efe2de1222bfa1229c86c275ebb09fb60a379b8
tree    2c0801830b5d28bb89748b61e232dd583766977f
parent  70ebff2e39c57a0606676211125c54d828f4a4eb
django_auto_sluggable /
name age
history
message
file LICENSE Loading commit data...
file README
file __init__.py
file models.py
README
=====================
django_auto_sluggable
=====================

Very simple Django extension that provides an auto-generated slug field to a Django model.

Use it something like:

----------------------------------------------------------------------------
from django.db import models
from django_auto_sluggable.models import SluggableModel

class MyModel(SluggableModel):
    name = models.CharField(max_length=255)
    
    def __unicode__(self):
        return self.name
----------------------------------------------------------------------------

The abstract model SluggableModel includes a slug field, and manages it for you. Note that the slug field is *not* 
marked as unique=True in the Django field definition, and hence will not have an index generated by default; you 
probably want to add one yourself.

When you create a new MyModel, its slug will be generated from the name field, unique across all instances.

If you instead want the slug to be unique across a subset of all your objects (say because your URLs look like 
/users/<username>/articles/<slug> and so the article slug only needs to be unique within a user) do something like:

----------------------------------------------------------------------------
from django.db import models
from django.contrib.auth.models import User
from django_auto_sluggable.models import SluggableModel

class Article(SluggableModel):
    name = models.CharField(max_length=255)
    user = models.ForeignKey(User)
    
    def slug_objects(self):
        return type(self).objects.filter(user=self.user)

    def __unicode__(self):
        return u"%s: %s" % (self.user, self.name,)
----------------------------------------------------------------------------

Finally, if you don't have a name field, you can do something like:

----------------------------------------------------------------------------
from django.db import models
from django_auto_sluggable.models import SluggableModel

class Paper(SluggableModel):
    title = models.CharField(max_length=255)
    
    @property
    def name(self):
        return self.title
    
    def __unicode__(self):
        return self.title
----------------------------------------------------------------------------

Alternatively, you could change SluggableModel itself to make the field used to generate the slug configurable; that's 
left as an exercise for the reader ;-)

James Aylett <http://tartarus.org/james/computers/django/>
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server