Completed
Push — master ( 0bffe1...d6203d )
by Diederik van der
11s
created

example.article.ArticleAdmin   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 28
Duplicated Lines 0 %
Metric Value
dl 0
loc 28
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A ArticleAdmin.get_prepopulated_fields() 0 3 1
1
from django.contrib import admin
2
from django.contrib.admin.widgets import AdminTextInputWidget, AdminTextareaWidget
3
from parler.admin import TranslatableAdmin, TranslatableStackedInline, TranslatableTabularInline
4
from .models import Article, Category, StackedCategory, TabularCategory
5
from parler.forms import TranslatableModelForm, TranslatedField
6
7
8
class ArticleAdminForm(TranslatableModelForm):
9
    """
10
    Example form
11
12
    Translated fields can be enhanced by manually declaring them:
13
    """
14
    title = TranslatedField(widget=AdminTextInputWidget)
15
    content = TranslatedField(widget=AdminTextareaWidget)
16
17
18
class ArticleAdmin(TranslatableAdmin):
19
    """
20
    Example admin.
21
22
    Using an empty class would already work,
23
    but this example shows some additional options.
24
    """
25
26
    # The 'language_column' is provided by the base class:
27
    list_display = ('title', 'language_column')
28
    list_filter = ('published',)
29
30
    # Example custom form usage.
31
    form = ArticleAdminForm
32
33
    # NOTE: when using Django 1.4, use declared_fieldsets= instead of fieldsets=
34
    fieldsets = (
35
        (None, {
36
            'fields': ('title', 'slug', 'published', 'category'),
37
        }),
38
        ("Contents", {
39
            'fields': ('content',),
40
        })
41
    )
42
43
    def get_prepopulated_fields(self, request, obj=None):
44
        # Can't use prepopulated_fields= yet, but this is a workaround.
45
        return {'slug': ('title',)}
46
47
48
class ArticleStacked(TranslatableStackedInline):
49
    model = Article
50
    extra = 1
51
52
53
class ArticleTabular(TranslatableTabularInline):
54
    model = Article
55
    extra = 1
56
57
58
class CategoryAdmin(admin.ModelAdmin):
59
    pass
60
61
62
class CategoryStackedAdmin(admin.ModelAdmin):
63
64
    inlines = [ArticleStacked]
65
66
67
class CategoryTabularAdmin(admin.ModelAdmin):
68
69
    inlines = [ArticleTabular]
70
71
72
admin.site.register(Article, ArticleAdmin)
73
admin.site.register(Category, CategoryAdmin)
74
admin.site.register(StackedCategory, CategoryStackedAdmin)
75
admin.site.register(TabularCategory, CategoryTabularAdmin)
76