Django Admin - Overriding the widget of a custom form field

╄→гoц情女王★ 提交于 2019-12-17 15:32:51

问题


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


回答1:


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)




回答2:


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

class ProductAdminForm(forms.ModelForm):
    class Meta:
        model = Product
        widgets = {
            'tags': admin.widgets.AdminTextareaWidget
        }

class ProductAdmin(admin.ModelAdmin):
    form = ProductAdminForm

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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/3469979/django-admin-overriding-the-widget-of-a-custom-form-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!