I'm new to python/django development and I'm stuck.
I'm making a project on Django 4.0.4. I have a tree table in a SQLite database.
In the admin panel, I configured it using the DraggableMPTTAdmin class from mptt. I also want to localize the fields of this table.
QUESTION: How can I combine the DraggableMPTTAdmin class with the TranslationAdmin class?
my_app/models.py
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
from django.db.models import CharField
class NonStrippingCharField(CharField):
"""A TextField that does not strip whitespace at the beginning/end of
it's value. Might be important for markup/code."""
def formfield(self, **kwargs):
kwargs['strip'] = False
return super(NonStrippingCharField, self).formfield(**kwargs)
class CategoriesAndAreas(MPTTModel):
name = NonStrippingCharField(max_length=150, unique=False)
is_category = models.BooleanField()
parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
def __str__(self):
return self.name
def __unicode__(self):
return self.name
class MPTTMeta:
order_insertion_by = ['name']
class Meta:
verbose_name = "Category or area"
verbose_name_plural = "Categories and areas"
my_app/translations.py
from modeltranslation.translator import register, TranslationOptions
from .models import CategoriesAndAreas
@register(CategoriesAndAreas)
class CategoriesAndAreasTranslationOptions(TranslationOptions):
fields = ('name',)
my_app/admin.py + DraggableMPTTAdmin
from django.contrib import admin
from mptt.admin import DraggableMPTTAdmin
from .models import CategoriesAndAreas
admin.site.register(CategoriesAndAreas, DraggableMPTTAdmin,
list_display=(
'tree_actions',
'indented_title',
'is_category',
),
list_display_links=(
'indented_title',
),
)
The table has a tree structure with the ability to drag and drop. The form for editing an element contains an extra field.
my_app/admin.py + TranslationAdmin
from django.contrib import admin
from modeltranslation.admin import TranslationAdmin
from .models import CategoriesAndAreas
admin.site.register(CategoriesAndAreas, TranslationAdmin)
The table structure is linear. Everything is fine here.
my_app/admin.py + TranslationAdmin + DraggableMPTTAdmin
from django.contrib import admin
from mptt.admin import DraggableMPTTAdmin
from modeltranslation.admin import TranslationAdmin
from .models import CategoriesAndAreas
class TranslationCategoriesAndAreas(CategoriesAndAreas, TranslationAdmin):
pass
admin.site.register(TranslationCategoriesAndAreas, DraggableMPTTAdmin,
list_display=(
'tree_actions',
'indented_title',
'is_category',
),
list_display_links=(
'indented_title',
),
)
ERROR: class TranslationCategoriesAndAreas(CategoriesAndAreas, TranslationAdmin): TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
I do not quite understand how the admin panel works, that's why I had difficulties. By trial and error, I came to the result. For an experienced developer, this would be easy...
my_app/admin.py + TranslationAdmin + DraggableMPTTAdmin
from django.contrib import admin
from mptt.admin import DraggableMPTTAdmin
from modeltranslation.admin import TranslationAdmin
from .models import CategoriesAndAreas
class DraggableTranslationAdmin(TranslationAdmin, DraggableMPTTAdmin):
pass
admin.site.register(CategoriesAndAreas, DraggableTranslationAdmin,
list_display=(
'tree_actions',
'indented_title',
'is_category',
),
list_display_links=(
'indented_title',
),
)