Django InlineModelAdmin:部分显示内联模型并链接到完整模型 [英] Django InlineModelAdmin: Show partially an inline model and link to the complete model
问题描述
我定义了几个模型:日记帐,卷,volume_scanInfo等。
日记可以有更多的卷,一个卷可以有更多的scanInfo。
我想做的是:
- 在期刊的管理页面我想要列表的卷内容(完成)
- 将前一个列表的每个卷连接到其管理页面,我可以在其中显示窗体来编辑卷和其扫描信息列表。
所以我想要这样的东西:
code> Journal#1 admin page
[name]
[publisher]
[url]
.....
卷内容列表
[volume 10] [..(其他字段)..]< a href =/ link / to / volume / 10>完整记录< / a>
[volume 20] [..(其他字段)..]< a href =/ link / to / volume / 20>完整记录< / a>
然后
卷#20管理页
[卷号]
[..(其他字段)...]
......
扫描列表info inline
[scan info 33] [..(其他字段)..]< a href =/ link / to / scaninfo / 33>完整记录< / a>
[scan info 44] [..(其他字段)..]< a href =/ link / to / scaninfo / 44>完整记录< / a>
我试图做的是定义一个创建代码并尝试在其中使用它的模型方法在管理员中定义内联卷的类,但不起作用。
换句话说
模型Volume的内容类似于:
def selflink(self):
return'< a href =/ admin / journaldb / volume /%s />完整记录< / a>'%self.vid
selflink.allow_tags = True
和
class VolumeInline(admin.TabularInline):
fields = ['volumenumber','selflink']
model = Volume
extra = 1
但是这会出现以下错误:
异常值:'VolumeInline.fields'是指字段'selflink 从表单中缺少。
任何想法?
谢谢,
Giovanni
更新:
自从Django 1.8开始, 。
OLD ANSWER:
最后我找到了一个简单的解决方案。
$ b
我创建一个名为 linked.html
的新模板,它是 tabular.html $ c $的副本c>我添加了这个代码来创建链接。
{%if inline_admin_form.original.pk%}
< td class ={{field.field.name}}>
< a href =/ admin / {{app_label}} / {{inline_admin_formset.opts.admin_model_path}} / {{inline_admin_form.original.pk}} />完整记录< / a>
< / td>
{%endif%}
然后我创建了一个新模型领导线
继承 InlineModelAdmin
#override of InlineModelAdmin支持表格式的内联链接
class LinkedInline(admin.options.InlineModelAdmin):
template =admin / linked.html
admin_model_path =无
def __init __(self,* args):
super(LinkedInline,self).__ init __(* args)
如果self.admin_model_path为None:
self.admin_model_path = self.model。 __name __。lower()
然后当我定义一个新的内联时,我只需要使用我的 LinkedInline
而不是通常的 InlineModelAdmin
。
我希望它可以对其他人有用。
Giovanni
I defined several models: Journals, volumes, volume_scanInfo etc.
A journal can have more volumes and a volume can have more scanInfo.
What I want to do is:
- in the admin page of journals I want to have the list of the volumes inline (done)
- connect each volume of the previous list to its admin page where I can show the form to edit the volume and the list of its "scan info" inline.
so I want to have something like:
Journal #1 admin page
[name]
[publisher]
[url]
.....
list of volumes inline
[volume 10] [..(other fields)..] <a href="/link/to/volume/10">Full record</a>
[volume 20] [..(other fields)..] <a href="/link/to/volume/20">Full record</a>
Then
Volume #20 admin page
[volume number]
[..(other fields)...]
......
list of the scan info inline
[scan info 33] [..(other fields)..] <a href="/link/to/scaninfo/33">Full record</a>
[scan info 44] [..(other fields)..] <a href="/link/to/scaninfo/44">Full record</a>
What I tried to do is defining a model method that create the code and trying to use it inside the class that defines "volume inline" in the admin, but it doesn't work.
In other words
the model "Volume" has inside something like:
def selflink(self):
return '<a href="/admin/journaldb/volume/%s/">Full record</a>' % self.vid
selflink.allow_tags = True
and
class VolumeInline(admin.TabularInline):
fields = ['volumenumber', 'selflink']
model = Volume
extra = 1
But this gives the following error:
Exception Value: 'VolumeInline.fields' refers to field 'selflink' that is missing from the form.
Any idea?
Thanks, Giovanni
UPDATE: Since Django 1.8, this is built in.
See this answer and the official documentation.
OLD ANSWER:
At the end I found a simple solution.
I create a new template called linked.html
that is a copy of tabular.html
and I added this code to create the link.
{% if inline_admin_form.original.pk %}
<td class="{{ field.field.name }}">
<a href="/admin/{{ app_label }}/{{ inline_admin_formset.opts.admin_model_path }}/{{ inline_admin_form.original.pk }}/">Full record</a>
</td>
{% endif %}
then I created a new model LinkedInline
inheriting InlineModelAdmin
#override of the InlineModelAdmin to support the link in the tabular inline
class LinkedInline(admin.options.InlineModelAdmin):
template = "admin/linked.html"
admin_model_path = None
def __init__(self, *args):
super(LinkedInline, self).__init__(*args)
if self.admin_model_path is None:
self.admin_model_path = self.model.__name__.lower()
Then when I define a new inline, I have only to use my LinkedInline
instead of the normal InlineModelAdmin
.
I hope it can be useful for other people.
Giovanni
这篇关于Django InlineModelAdmin:部分显示内联模型并链接到完整模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!