在django管理员中将参数传递给内联表单 [英] Passing parameters to inline form in django admin
本文介绍了在django管理员中将参数传递给内联表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class TemplateAdmin(admin.ModelAdmin):
inlines = (TemplateAttributeInline,CompanyAttributeInline)
list_display =(name,created,updated,departments)
list_filter = ['companies__company']
list_editable =(departments )
search_fields =(name,companies__company,)
exclude =(companies,)
save_as = True
我想将参数传递给 TemplateAttributeInline
,然后将参数传递给 TemplateAttributeForm
。什么是最好的方法?
TemplateAttributeInline:
class TemplateAttributeInline(admin.TabularInline):
model = TemplateAttribute
extra = 0
sortable_field_name =display
form = TemplateAttributeForm
TemplateAttributeForm
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super super (TemplateAttributeForm,self).__ init __(* args,** kwargs)
self.fields ['attribute']。queryset = Attribute.objects.filter(#要向公司的代码过滤模板我们在ADMIN页面上编辑
解决方案
您可以创建一个函数返回一个表单的类:
def TemplateAttributeForm(param):
class MyTemplateAttributeForm(forms.ModelForm):
class Meta:
model = Template
def __init __(self,* args,** kwargs):
super(TemplateAttributeForm,self)。 __init __(* args,** kwargs)
#do你想要什么与param
返回MyTemplateAttributeForm
在另一个功能中使用它来定义 TemplateAttributeInline
def TemplateAttributeInline(param):
class MyTemplateAttributeInline(admin.TabularInline):
model = TemplateAttribute
extra = 0
sortable_field_name =display
form = TemplateAttributeForm(param)
返回MyTemplateAttributeInline
要完成,请在 TemplateAdmin
定义中使用此功能:
类TemplateAdmin (admin.ModelAdmin):
inlin es =(TemplateAttributeInline(param),CompanyAttributeInline)
....
I have an admin class inheriting from ModelAdmin:
class TemplateAdmin (admin.ModelAdmin):
inlines = (TemplateAttributeInline, CompanyAttributeInline)
list_display = ("name", "created", "updated","departments")
list_filter = ['companies__company']
list_editable = ("departments",)
search_fields = ("name", "companies__company",)
exclude = ("companies",)
save_as = True
I want to pass a parameter to TemplateAttributeInline
which will in turn then pass the parameter to TemplateAttributeForm
. What is the best way to do this?
TemplateAttributeInline:
class TemplateAttributeInline (admin.TabularInline):
model = TemplateAttribute
extra = 0
sortable_field_name = "display"
form = TemplateAttributeForm
TemplateAttributeForm
class TemplateAttributeForm(forms.ModelForm):
class Meta:
model = Template
def __init__(self,*args, **kwargs):
super(TemplateAttributeForm, self).__init__(*args, **kwargs)
self.fields['attribute'].queryset = Attribute.objects.filter(#WANT TO FILTER BY THE ID OF THE COMPANY THAT OWNS THE TEMPLATE WE ARE EDITING ON THE ADMIN PAGE)
解决方案
You can create a function that returns a class for the form:
def TemplateAttributeForm(param):
class MyTemplateAttributeForm(forms.ModelForm):
class Meta:
model = Template
def __init__(self,*args, **kwargs):
super(TemplateAttributeForm, self).__init__(*args, **kwargs)
#do what ever you want with param
return MyTemplateAttributeForm
Use it in another funtion to define the TemplateAttributeInline
def TemplateAttributeInline(param):
class MyTemplateAttributeInline (admin.TabularInline):
model = TemplateAttribute
extra = 0
sortable_field_name = "display"
form = TemplateAttributeForm(param)
return MyTemplateAttributeInline
To finish, use this function in your TemplateAdmin
definition:
class TemplateAdmin (admin.ModelAdmin):
inlines = (TemplateAttributeInline(param), CompanyAttributeInline)
....
这篇关于在django管理员中将参数传递给内联表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文