Integrating translations in the administration site

django-parler integrates smoothly with the Django administration site. It includes a TranslatableAdmin class that overrides the ModelAdmin class provided by Django to manage model translations.

Edit the admin.py file of the shop application and add the following import to it:

from parler.admin import TranslatableAdmin

Modify the CategoryAdmin and ProductAdmin classes to inherit from TranslatableAdmin instead of ModelAdmin. django-parler doesn't support the prepopulated_fields attribute, but it does support the get_prepopulated_fields() method that provides the same functionality. Let's change this accordingly. Edit the admin.py file to make it look as follows:

from django.contrib import admin
from .models import Category, Product
from parler.admin import TranslatableAdmin

@admin.register(Category)
class CategoryAdmin(TranslatableAdmin):
list_display = ['name', 'slug']

def get_prepopulated_fields(self, request, obj=None):
return {'slug': ('name',)}

@admin.register(Product)
class ProductAdmin(TranslatableAdmin):
list_display = ['name', 'slug', 'price',
'available', 'created', 'updated']
list_filter = ['available', 'created', 'updated']
list_editable = ['price', 'available']

def get_prepopulated_fields(self, request, obj=None):
return {'slug': ('name',)}

We have adapted the administration site to work with the new translated models. We can now sync the database with the model changes that we made.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset