具有树状结构的模型的本地化

Localization of the model with a tree-like structure

我是 python/django 开发的新手,我被卡住了。

我正在 Django 4.0.4 上做一个项目。 我在 SQLite 数据库中有一棵树 table。

在管理面板中,我使用来自 mpttDraggableMPTTAdmin class 配置它。我也想本地化这个 table.

的字段

问题:如何将 DraggableMPTTAdmin class 与 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

我不太明白管理面板是如何工作的,这就是我遇到困难的原因。 通过反复试验,我得出了结果。对于有经验的开发人员来说,这很容易...

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',
    ),
)