更改Django Admin的“添加另一个SomeObject”按钮上的文本

9avjhtql  于 2022-11-26  发布在  Go
关注(0)|答案(2)|浏览(126)

通过下面的代码,我得到了一个Django Admin UI,我可以在Person页面上添加一些度量:

链接/按钮显示文本“添加另一个测量”。如何更改该文本?
编码:

class Person(models.Model):
    class Meta:
        db_table = 'people'
        verbose_name = "Person"
        verbose_name_plural = "People"

    name = models.CharField(max_length=100, null=False, blank=False)
   

class Measurement(models.Model):
    class Meta:
        db_table = 'measurements'
        verbose_name = "Measurement"
        verbose_name_plural = "Measurements"

    value = models.IntegerField(null=False)
    person = models.ForeignKey(Person, null=False, on_delete=CASCADE)

class MeasurementInline(InlineModelAdmin):
    model = Measurement
    extra = 0

class PersonAdmin(admin.ModelAdmin):
    fields = ('name',)
    list_display = ('name',)

    inlines = [MeasurementInline]
ni65a41a

ni65a41a1#

通过将verbose_nameverbose_name_plural添加到MeasurementInline,我设法更改了文本(在本例中以小写“m”开头):

class MeasurementInline(InlineModelAdmin):
    model = Measurement
    extra = 0
    verbose_name = 'measurement'
    verbose_name_plural = 'measurements'
zaq34kh6

zaq34kh62#

我不喜欢这个解决方案。所以对于每一个想改变整个文本“添加另一个{obj_verbose_name}”的人,这里是我想到的

class InlineAdminFormSetOnlyAddTextInAddInline(helpers.InlineAdminFormSet):
def inline_formset_data(self):
    return json.dumps({
        'name': '#%s' % self.formset.prefix,
        'options': {
            'prefix': self.formset.prefix,
            'addText': gettext('Add'),
            'deleteText': gettext('Remove'),
        }
    })

在这里我实现了BaseAdmin类,您需要覆盖 get_inline_formsets -方法
就像这样:

def get_inline_formsets(self, request, formsets, inline_instances, obj=None):

      can_edit_parent = self.has_change_permission(request, obj) if obj else self.has_add_permission(request)
    inline_admin_formsets = []
    for inline, formset in zip(inline_instances, formsets):
        fieldsets = list(inline.get_fieldsets(request, obj))
        readonly = list(inline.get_readonly_fields(request, obj))
        if can_edit_parent:
            has_add_permission = inline.has_add_permission(request, obj)
            has_change_permission = inline.has_change_permission(request, obj)
            has_delete_permission = inline.has_delete_permission(request, obj)
        else:
            # Disable all edit-permissions, and overide formset settings.
            has_add_permission = has_change_permission = has_delete_permission = False
            formset.extra = formset.max_num = 0
        has_view_permission = inline.has_view_permission(request, obj)
        prepopulated = dict(inline.get_prepopulated_fields(request, obj))
        inline_admin_formset = InlineAdminFormSetOnlyAddTextInAddInline(
            inline, formset, fieldsets, prepopulated, readonly, model_admin=self,
            has_add_permission=has_add_permission, has_change_permission=has_change_permission,
            has_delete_permission=has_delete_permission, has_view_permission=has_view_permission,
        )
        inline_admin_formsets.append(inline_admin_formset)

或类似于:

def get_inline_formsets(self, request, formsets, inline_instances, obj=None):
   formsets_ = super().get_inline_formsets(request, formsets, inline_instances, obj)
   for formset_obj in formsets_:
      formset_obj.__class__ = InlineAdminFormSetOnlyAddTextInAddInline
   return formsets_

如果有人知道更好的解决办法,我很高兴听到它。

相关问题