我定义了几个模型:日志(log)、卷、volume_scanInfo等.

日志(log)可以有更多卷,卷可以有更多scanInfo.

我想做的是:

  • 在期刊的管理页面中,我想让卷列表内联(完成)
  • connect each volume of the previous list to its admin page where I can show the form to edit the volume 和 the list of its "scan info" inline.

所以我想要像这样的东西:

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>

然后

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>

我try 做的是定义一个创建代码的模型方法,并try 在定义管理员中"内联卷"的类中使用它,但它不起作用.

换句话说

"体积"模型的内部 struct 如下:

def selflink(self):
    return '<a href="/admin/journaldb/volume/%s/">Full record</a>' % self.vid
selflink.allow_tags = True

class VolumeInline(admin.TabularInline):
    fields = ['volumenumber', 'selflink']
    model = Volume
    extra = 1

但这会产生以下错误:

Exception Value: 'VolumeInline.fields' refers to field 'selflink' that is missing from the form.

有什么 idea 吗?

谢谢, 乔瓦尼

推荐答案

UPDATE:个 从Django 1.8开始,这是内置的.

this answerthe official documentation.

OLD ANSWER:

最后我找到了一个简单的解决方案.

我创建了一个名为linked.html的新模板,它是tabular.html的副本,我添加了以下代码来创建链接.

{% 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 %}

然后我创建了一个继承InlineModelAdmin的新模型LinkedInline

#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()

然后,当我定义一个新的内联时,我只需要使用我的LinkedInline而不是普通的InlineModelAdmin.

我希望它能对其他人有用.

乔瓦尼

Django相关问答推荐

Django中有修改字段值的挂钩吗?

构造一个定制字符串(实体、年份和字母数字的组合)作为Django中的Postgres数据库的主键是否明智?

使用自定义模型注册后,Django无法登录

无法在views.py django中验证密码

从多个数据库访问 Django 会话

如何在 Fargate 容器中运行的 Django 中使用 AWS SES?

如何根据状态计算表中数据的数量?

如何在 Django REST Framework API 响应中使用多标签过滤器?

在 Django Admin change_list 视图中更改 list_editable 字段时保存 Django 模型

Django ORM 和锁定表

Python / Django 中的 Unicode 与 UTF-8 混淆?

将现有 auth.User 数据迁移到新的 Django 1.5 自定义用户模型?

如何在 Django 中返回静态 HTML 文件作为响应?

如何在 Django 中向 ModelForm 添加外键字段?

在python中查找对对象的所有引用

如何在不同的元素上添加注释?

Django unique_together 与可为空的 ForeignKey

UpdateView 中的success_url,基于传递的值

Django中reverse()和reverse_lazy()的区别

在 Django 中使用 select_related Select 特定字段