Skip to content

Commit

Permalink
Use admin site autodiscovery. Extracted admin code into admin.py. Sho…
Browse files Browse the repository at this point in the history
…w off new

ModelAdmin translation code.
  • Loading branch information
joost@cassee.net committed Jul 23, 2008
1 parent 3047e32 commit 8ef73d1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
22 changes: 22 additions & 0 deletions testproject/articles/admin.py
@@ -0,0 +1,22 @@
from django import forms
from django.contrib import admin
from multilingual import translation
from articles import models

class CategoryAdmin(admin.ModelAdmin):
# Field names would just work here, but if you need
# correct list headers (from field.verbose_name) you have to
# use the get_'field_name' functions here.
list_display = ('id', 'creator', 'created', 'name', 'description')
search_fields = ('name', 'description')

class ArticleAdmin(admin.ModelAdmin):
class Translation(translation.TranslationModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
field = super(translation.TranslationModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)
if db_field.name == 'contents':
field.widget = forms.Textarea(attrs={'cols': 50})
return field

admin.site.register(models.Article, ArticleAdmin)
admin.site.register(models.Category, CategoryAdmin)
23 changes: 4 additions & 19 deletions testproject/articles/models.py
Expand Up @@ -141,7 +141,7 @@
['Fixture kategoria', 'kategoria 2']
### Check specifying query set language
>>> c_en = Category.objects.all().for_language('en')
>>> c_en = Category.objects.all().for_language('en')
>>> c_pl = Category.objects.all().for_language(2) # both ID and code work here
>>> to_str(c_en.get(name__contains='1').name)
'zzz cat 1'
Expand Down Expand Up @@ -225,7 +225,7 @@ class Category(models.Model):
"""
Test model for multilingual content: a simplified Category.
"""

# First, some fields that do not need translations
creator = models.ForeignKey(User, verbose_name=_("Created by"),
blank=True, null=True)
Expand All @@ -241,7 +241,7 @@ class Translation(multilingual.Translation):
The multilingual machinery will automatically add these to the
Category class:
* get_name(language_id=None)
* set_name(value, language_id=None)
* get_description(language_id=None)
Expand Down Expand Up @@ -270,15 +270,6 @@ def __unicode__(self):
def __str__(self):
# compatibility
return str(self.__unicode__())

class Admin:
# Again, field names would just work here, but if you need
# correct list headers (from field.verbose_name) you have to
# use the get_'field_name' functions here.

# Note: this Admin class does not do anything in newforms-admin
list_display = ('id', 'creator', 'created', 'name', 'description')
search_fields = ('name', 'description')

class Meta:
verbose_name_plural = 'categories'
Expand All @@ -302,16 +293,10 @@ class Article(models.Model):
blank=True, null=True)

objects = CustomArticleManager()

# And now the translatable fields
class Translation(multilingual.Translation):
title = models.CharField(verbose_name=_("The title"),
blank=True, null=False, max_length=250)
contents = models.TextField(verbose_name=_("The contents"),
blank=True, null=False)

from multilingual.compat import IS_NEWFORMS_ADMIN
if IS_NEWFORMS_ADMIN:
from django.contrib import admin
admin.site.register(Article)
admin.site.register(Category)
19 changes: 5 additions & 14 deletions testproject/urls.py
@@ -1,6 +1,9 @@
from django.conf.urls.defaults import *
from django.contrib import admin
from articles.models import *

admin.autodiscover()

urlpatterns = patterns('',
(r'^$', 'django.views.generic.list_detail.object_list',
{'queryset': Category.objects.all(),
Expand All @@ -10,18 +13,6 @@
'post_save_redirect': '/'}),
(r'^new/$', 'django.views.generic.create_update.create_object',
{'model': Category}),
)

# handle both newforms and oldforms admin URL

from multilingual.compat import IS_NEWFORMS_ADMIN
if IS_NEWFORMS_ADMIN:
from django.contrib import admin

urlpatterns += patterns('',
(r'^admin/(.*)', admin.site.root),
)
else:
urlpatterns += patterns('',
(r'^admin/', include('django.contrib.admin.urls')),
)
(r'^admin/(.*)', admin.site.root),
)

0 comments on commit 8ef73d1

Please sign in to comment.