Django Admin - 覆盖自定义表单字段的小部件

发布于 2024-09-14 18:14:08 字数 654 浏览 7 评论 0原文

我有一个自定义 TagField 表单字段。

class TagField(forms.CharField):
    def __init__(self, *args, **kwargs):
        super(TagField, self).__init__(*args, **kwargs)
        self.widget = forms.TextInput(attrs={'class':'tag_field'})

如上所示,它使用 TextInput 表单字段小部件。但在管理中我希望它使用 Textarea 小部件显示。为此,有 formfield_overrides 挂钩,但它不适用于这种情况。

管理员声明是:

class ProductAdmin(admin.ModelAdmin):
    ...
    formfield_overrides = {
        TagField: {'widget': admin.widgets.AdminTextareaWidget},
    }

这对表单字段小部件没有影响,并且 标签 仍然使用 TextInput 小部件呈现。

非常感谢任何帮助。

--
奥马特

I have a custom TagField form field.

class TagField(forms.CharField):
    def __init__(self, *args, **kwargs):
        super(TagField, self).__init__(*args, **kwargs)
        self.widget = forms.TextInput(attrs={'class':'tag_field'})

As seen above, it uses a TextInput form field widget. But in admin I would like it to be displayed using Textarea widget. For this, there is formfield_overrides hook but it does not work for this case.

The admin declaration is:

class ProductAdmin(admin.ModelAdmin):
    ...
    formfield_overrides = {
        TagField: {'widget': admin.widgets.AdminTextareaWidget},
    }

This has no effect on the form field widget and tags are still rendered with a TextInput widget.

Any help is much appreciated.

--
omat

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

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

发布评论

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

评论(4

我很OK 2024-09-21 18:14:08

django 管理员在其许多字段中使用自定义小部件。覆盖字段的方法是创建一个与 ModelAdmin 对象一起使用的表单。

# 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'].widget = admin.widgets.AdminTextareaWidget()

然后,在 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)

此时,您还可以覆盖查询集:例如,根据模型中的另一个字段过滤对象(因为 limit_choices_to 无法处理此问题)

The django admin uses custom widgets for many of its fields. The way to override fields is to create a Form for use with the ModelAdmin object.

# 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'].widget = admin.widgets.AdminTextareaWidget()

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)

You can also override the queryset at this time: to filter objects according to another field in the model, for instance (since limit_choices_to cannot handle this)

叶落知秋 2024-09-21 18:14:08

从 Django 1.2 开始,您可以通过扩展 ModelFormMeta 类来覆盖字段小部件:

from django import forms
from django.contrib import admin


class ProductAdminForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = '__all__'  # edit: django >= 1.8
        widgets = {
            'tags': admin.widgets.AdminTextareaWidget
        }


class ProductAdmin(admin.ModelAdmin):
    form = ProductAdminForm

https://docs.djangoproject.com/en/stable/topics/forms/modelforms/#overriding-the-default-fields

You can override field widgets by extending the Meta class of a ModelForm since Django 1.2:

from django import forms
from django.contrib import admin


class ProductAdminForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = '__all__'  # edit: django >= 1.8
        widgets = {
            'tags': admin.widgets.AdminTextareaWidget
        }


class ProductAdmin(admin.ModelAdmin):
    form = ProductAdminForm

https://docs.djangoproject.com/en/stable/topics/forms/modelforms/#overriding-the-default-fields

梦幻的心爱 2024-09-21 18:14:08

对于特定字段而不是我使用的字段:

Django 2.1.7

class ProjectAdminForm(forms.ModelForm):
    class Meta:
        model = Project
        fields = '__all__'
        widgets = {
            'project_description': forms.Textarea(attrs={'cols': 98})
        }
class ProjectAdmin(admin.ModelAdmin):
    form = ProjectAdminForm

谢谢,@Murat Çorlu

For a specific field not a kind of fields I use:

Django 2.1.7

class ProjectAdminForm(forms.ModelForm):
    class Meta:
        model = Project
        fields = '__all__'
        widgets = {
            'project_description': forms.Textarea(attrs={'cols': 98})
        }
class ProjectAdmin(admin.ModelAdmin):
    form = ProjectAdminForm

Thanks, @Murat Çorlu

爱你是孤单的心事 2024-09-21 18:14:08

尝试像这样更改您的字段:

class TagField(forms.CharField):
    def __init__(self, *args, **kwargs):
        self.widget = forms.TextInput(attrs={'class':'tag_field'})
        super(TagField, self).__init__(*args, **kwargs)

这将允许使用来自 **kwargs 的小部件。否则,您的字段将始终使用 form.TextInput 小部件。

Try to change your field like this:

class TagField(forms.CharField):
    def __init__(self, *args, **kwargs):
        self.widget = forms.TextInput(attrs={'class':'tag_field'})
        super(TagField, self).__init__(*args, **kwargs)

This would allow to use the widget which comes from **kwargs. Otherwise your field will always use form.TextInput widget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文