在 django admin 中自定义选择

发布于 2024-10-27 00:34:09 字数 588 浏览 9 评论 0原文

我有一个模型,其中一个字段是外键,所以我在 django admin 中看到选择,是否可以自定义此选择的标签?



class Model(models.Model):
    name = models.CharField()
    def __unicode__(self):
         return self.name

class Part(models.Model):
    name = models.CharField()
    parent =  model.ForeignKey(Model)
    def __unicode__(self):
         return self.name
    def name_with_model(self):
         return self.name + ' ' + parent.name

class SmallPart(models.Model):
    name = models.CharField()
    parent =  model.ForeignKey(Part)

当我添加新的 SmallPart 时,我看到带有零件名称的选择标签,我需要看到 name_with_model

I have a model, one field of it is a ForeignKey, so i see select in django admin, is it possiable to customize labels of this select?



class Model(models.Model):
    name = models.CharField()
    def __unicode__(self):
         return self.name

class Part(models.Model):
    name = models.CharField()
    parent =  model.ForeignKey(Model)
    def __unicode__(self):
         return self.name
    def name_with_model(self):
         return self.name + ' ' + parent.name

class SmallPart(models.Model):
    name = models.CharField()
    parent =  model.ForeignKey(Part)

when I add new SmallPart I see select tag with names of parts, I need to see name_with_model

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2024-11-03 00:34:09

如果您的意思是字段标签:

使用以下代码:
Django 管理 - 覆盖 a 的小部件自定义表单字段

# forms.py

from django import forms
from django.contrib import admin

class ProductAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ProductAdminForm, self).__init__(*args, **kwargs)
        self.fields['tags'].label = 'Custom Label'

然后,在 ModelAdmin 对象中指定表单:

from django.contrib import admin
from models import Product
from forms import ProductAdminForm

class ProductAdmin(admin.ModelAdmin):
    form = ProductAdminForm

admin.site.register(Product, ProductAdmin)

如果您指的是选择下拉列表中的标签:

覆盖小部件,如上面的答案所示。

编辑:

fk 字段的默认表单字段是模型选择字段。来自文档

模型的unicode方法
将被调用以生成字符串
使用对象的表示
在领域的选择上;提供
定制表示,子类
ModelChoiceField 和覆盖
label_from_instance。这个方法将
接收一个模型对象,并且应该
返回一个适合的字符串
代表它。例如:

class MyModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.name_with_model()

然后:

class SmallPartAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(SmallPartAdminForm, self).__init__(*args, **kwargs)
        self.fields['parent'] = MyModelChoiceField(queryset=Part.objects.all())

If you mean the field label:

using code from:
Django Admin - Overriding the widget of a custom form field

# forms.py

from django import forms
from django.contrib import admin

class ProductAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ProductAdminForm, self).__init__(*args, **kwargs)
        self.fields['tags'].label = 'Custom Label'

Then, in your ModelAdmin object, you specify the form:

from django.contrib import admin
from models import Product
from forms import ProductAdminForm

class ProductAdmin(admin.ModelAdmin):
    form = ProductAdminForm

admin.site.register(Product, ProductAdmin)

If you mean the labels in the select drop down:

Override the widget like in the answer above.

edit:

The default form field for a fk field is a model choice field. From the docs

The unicode method of the model
will be called to generate string
representations of the objects for use
in the field's choices; to provide
customized representations, subclass
ModelChoiceField and override
label_from_instance. This method will
receive a model object, and should
return a string suitable for
representing it. For example:

class MyModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.name_with_model()

and then:

class SmallPartAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(SmallPartAdminForm, self).__init__(*args, **kwargs)
        self.fields['parent'] = MyModelChoiceField(queryset=Part.objects.all())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文