Search code examples
pythondjangourlweb-applicationsslug

Can I use more than one slug field for a model on Django?


I want to use an slug to refer an url. Nonetheless, the model I want to use is design for a chinese vocabulary quiz and I want to use 3 different slugs to refers the url.

Then, I have a model

models.py

from django.db import models
from django.db.models import permalink
from django.template.defaultfilters import slugify
from unidecode import unidecode

class Vocabulary( models.Model ):

title = models.CharField(" Title ",  max_length = 100 )
vocabularyCharacter = models.CharField("Character",  max_length = 100, blank = True, null = True )
vocabularyTranslation = models.CharField("Translation",  max_length = 100, blank = True, null = True )
vocabularyPinyin = models.CharField("Pinyin",  max_length = 100, blank = True, null = True )
vocabularyCharacterSlug = models.SlugField( "Character Slug (optional)",  max_length = 100, blank = True, null = True )
vocabularyTranslationSlug = models.SlugField("Translation Slug (optional)",  max_length = 100, blank = True, null = True )
vocabularyPinyinSlug = models.SlugField("Pinyin Slug (optional)",  max_length = 100, blank = True, null = True )
audio_file = models.FileField("Audio file (optional)", upload_to = u'resources/mp3/', max_length=200, blank = True, null=True)
description = models.TextField("Description ",  blank = True, null = True )

class Meta:
    verbose_name = 'vocabulary'
    verbose_name_plural = 'vocabularies'

def __unicode__( self ):
    return u"%s" % self.title

def save( self, force_insert = False, force_update = False ):
    self.vocabularyCharacterSlug = slugify(unidecode(self.vocabularyCharacter))
    self.vocabularyTranslationSlug = slugify(unidecode(self.vocabularyTranslation))
    self.vocabularyPinyinSlug = slugify(unidecode(self.vocabularyPinyin))
    super( Vocabulary, self ).save( force_insert, force_update )

@permalink
def get_absolute_vocabulary_character_url( self ):
    return ( 'vocabulary_character_detail', None, {
        'vocabularyCharacterSlug':    self.vocabularyCharacterSlug,
    } )

@permalink
def get_absolute_vocabulary_traslation_url( self ):
    return ( 'vocabulary_traslation_detail', None, {
        'vocabularyTranslationSlug':    self.vocabularyTranslationSlug,
    } )

@permalink
def get_absolute_vocabulary_pinyin_url( self ):
    return ( 'vocabulary_pinyin_detail', None, {
        'vocabularyPinyinSlug':    self.vocabularyPinyinSlug,
    } )

Then, I have a view for this model

view.py

from django.shortcuts import get_object_or_404, render_to_response
from core.vocabulary.models import Vocabulary

def vocabulary_character_detail( request, vocabularyCharacterSlug ):

    vocabulary = get_object_or_404( Vocabulary, vocabularyCharacterSlug = vocabularyCharacterSlug )
    return render_to_response( 'vocabulary/vocabulary_character_detail.html',
                              {'vocabulary': vocabulary} )

def vocabulary_translation_detail( request, vocabularyTranslationSlug ):

    vocabulary = get_object_or_404( Vocabulary, vocabularyTranslationSlug = vocabularyTranslationSlug )
    return render_to_response( 'vocabulary/vocabulary_translation_detail.html',
                              {'vocabulary': vocabulary} )

def vocabulary_pinyin_detail( request, vocabularyPinyinSlug ):

    vocabulary = get_object_or_404( Vocabulary, vocabularyPinyinSlug = vocabularyPinyinSlug )
    return render_to_response( 'vocabulary/vocabulary_pinyin_detail.html',
                              {'vocabulary': vocabulary} )

Additionally, I have this url file

urls.py

from django.conf.urls import patterns, url
from core.vocabulary.views.vocabulary import vocabulary_character_detail
from core.vocabulary.views.vocabulary import vocabulary_translation_detail, vocabulary_pinyin_detail

urlpatterns = patterns( '',     
        url( r'^(?P<vocabularyCharacterSlug>[-\w]+)/$',
            vocabulary_character_detail,
            name = 'vocabulary_character' ),

        url( r'^(?P<vocabularyTranslationSlug>[-\w]+)/$',
            vocabulary_translation_detail,
            name = 'vocabulary_translation' ),

        url( r'^(?P<vocabularyPinyinSlug>[-\w]+)/$',
            vocabulary_pinyin_detail,
            name = 'vocabulary_pinyin' ),
 )

I created the corresponding templates and I can access the url

url( r'^(?P<vocabularyCharacterSlug>[-\w]+)/$',
            vocabulary_character_detail,
            name = 'vocabulary_character' ),

And the other 2, I received an error saying

No Vocabulary matches the given query.

Does Django only support only one slug field for model?

If Django support more than one slug field, what am I missing?


Solution

  • Why not point your views to different directories?

    urlpatterns = patterns( '',     
        url( r'^character/(?P<vocabularyCharacterSlug>[-\w]+)/$',
            vocabulary_character_detail,
            name = 'vocabulary_character' ),
    
        url( r'^translation/(?P<vocabularyTranslationSlug>[-\w]+)/$',
            vocabulary_translation_detail,
            name = 'vocabulary_translation' ),
    
        url( r'^pinyin/(?P<vocabularyPinyinSlug>[-\w]+)/$',
            vocabulary_pinyin_detail,
            name = 'vocabulary_pinyin' ),
    

    )

    model:

    def vocabulary_character_detail( request, vocabularyCharacterSlug ):
    
       vocabulary = get_object_or_404( Vocabulary, vocabularyCharacterSlug = vocabularyCharacterSlug )
       return render_to_response( 'vocabulary/character/vocabulary_character_detail.html',
                              {'vocabulary': vocabulary} )
    
    def vocabulary_translation_detail( request, vocabularyTranslationSlug ):
    
       vocabulary = get_object_or_404( Vocabulary, vocabularyTranslationSlug = vocabularyTranslationSlug )
       return render_to_response( 'vocabulary/translation/vocabulary_translation_detail.html',
                              {'vocabulary': vocabulary} )
    
    def vocabulary_pinyin_detail( request, vocabularyPinyinSlug ):
    
       vocabulary = get_object_or_404( Vocabulary, vocabularyPinyinSlug = vocabularyPinyinSlug )
       return render_to_response( 'vocabulary/pinyin/vocabulary_pinyin_detail.html',
                              {'vocabulary': vocabulary}