gpt4 book ai didi

Django 管理员 : change displayed column name in inline ManyToMany field

转载 作者:行者123 更新时间:2023-12-02 01:36:21 24 4
gpt4 key购买 nike

我尝试翻译 Django 管理站点,但 ManyToMany TabularInline 遇到问题。我的 models.py 是:

class Doctor(models.Model):
(...)
specializations = models.ManyToManyField(Specialization, blank=True, verbose_name='Specjalizacje')

class Meta:
verbose_name = 'Lekarz'
verbose_name_plural = 'Lekarze'

class Specialization(models.Model):
name = models.CharField(max_length=191, verbose_name='Nazwa')

class Meta:
verbose_name = 'Specjalizacja'
verbose_name_plural = 'Specjalizacje'

我的 admin.py 看起来像:

class SpecializationInline(admin.TabularInline):
model = Doctor.specializations.through
verbose_name = 'Specjalizacja'
verbose_name_plural = 'Specjalizacja'

@admin.register(Specialization)
class SpecializationAdmin(admin.ModelAdmin):
list_display = ['name',]

@admin.register(Doctor)
class DoctorAdmin(admin.ModelAdmin):
inlines = [SpecializationInline,]

# this field is added as inline
exclude = ['specializations',]

生成的 Django 管理页面如下所示: django admin page view

除了“特化”列之外,所有内容均已翻译。如何更改其名称?

最佳答案

好吧,这不是一个完整的解决方案。但我希望这对你有帮助。在 TabularInline 调用的模板中,admin/edit_inline/tabular.html

构建表格“标题”部分的部分

 <thead><tr>
...
{% for field in inline_admin_formset.fields %}
{% if not field.widget.is_hidden %}
<th{% if field.required %} class="required"{% endif %}>
{{ field.label|capfirst }}
...
</th>
{% endif %}
{% endfor %}
...
</tr></thead>

这告诉我您可以向字段添加标签。因此,通过创建自定义表单

class SpecializationForm(ModelForm):
specializations = forms.ModelChoiceField(queryset=Specialization.objects.all(), label='Specjalizacja')

class Meta:
model = Specialization
fields = ('specializations', )

并在您的SpecializationInline中调用它

class SpecializationInline(admin.TabularInline):
model = Doctor.specializations.through
verbose_name = 'Specjalizacja'
verbose_name_plural = 'Specjalizacja'
form = SpecializationForm

这应该允许您更改文本。

关于Django 管理员 : change displayed column name in inline ManyToMany field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48169615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com