使用 django 中的 admin 进行主/详细信息

发布于 2024-11-09 17:37:14 字数 952 浏览 4 评论 0原文

我用谷歌搜索并查看了文档,但没有找到任何东西 与此场景相关:

我有一个主/详细模型,定义如下:

class Master(models.Model):
   title = models.CharField(max_length=100)

   def details_url(self):
       return '<a href="here comes the url linking to details">Details</a>'
   details_url.short_description = 'Link to details'
   details_url.allow_tags = True

class Detail(models.Model):
    master = models.ForeignKey(Master)
    details = models.TextField()

它们使用 admin.py 在管理中公开。我的问题是这样的:

1)如何在主模型中创建details_url属性,以便 当在 admin.py 中公开时,用户可以单击它并转到以下列表 详细信息并且只能看到与该主模型实例相关的详细信息?

2)如果这只能使用自定义视图来完成,我如何获取视图 生成一个看起来像管理员其他部分的页面?我知道我可以使用 与管理员相同的模板代码,但是有没有更快的方法 例如,生成像我的案例中的详细信息这样的列表吗?通用的 视图/课程?

3)将Master添加到detailsAdmin类的list_filter中不是 由于主记录的数量,这是可能的。或者说是……?甚至 如果可能的话,我如何从另一个页面设置过滤器,就像我 单击主模型的change_list 中的链接转到详细信息 change_list,如何将过滤设置为特定 主模型的实例?

简而言之;我想要的只是将change_list过滤为仅显示 与特定主记录相关的详细信息并尽可能多地重复使用 管理代码/功能库尽可能。

I've googled and looked at the docs, but haven't found anything
related to this scenario:

I got a master/detail model, defined like so:

class Master(models.Model):
   title = models.CharField(max_length=100)

   def details_url(self):
       return '<a href="here comes the url linking to details">Details</a>'
   details_url.short_description = 'Link to details'
   details_url.allow_tags = True

class Detail(models.Model):
    master = models.ForeignKey(Master)
    details = models.TextField()

They're exposed in the admin using admin.py. My question is this:

1) How can I make the details_url property in the Master-model so that
when exposed in the admin.py a user can click it and go the listing of
details and only see details related to that instance of Master-model?

2) If this can only be done using a custom view, how do I get the view
to produce a page looking like the rest of the admin? I know I can use
the same template code as the admin, but is there a faster way to
produce for instance listings like the details in my case? Generic
views/classes?

3) Adding Master to list_filter for the detailsAdmin-class is not
possible due to the amount of Master-records. Or is it ...? And even
if it was possible, how can I set filters from another page, like if I
click a link in the change_list for Master-model going to the Details
change_list, how can I set the filtering to be set to a specific
instance of a Master model?

In short; all I want is the have the change_list filtered to only show
details related to a specific master-record and reuse as much of the
admin-code/featurebase as possible.

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

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

发布评论

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

评论(1

痴者 2024-11-16 17:37:14

在 Django 1.2.4 之前,您可以使用 GET 查询在管理员中创建指向模型的过滤链接,如下所示:http://your_site.com/admin/your_app/detail/?master__id__exact=2

但这是一个安全漏洞,并已得到修复。现在,如果您尝试使用 list_filter 属性中未指定的查找来过滤模型,您将收到 SuspiciousOperation 异常。

尽管有一个解决办法。 此解决方法实现了 valid_lookups 属性,以便您可以通过 URL get 查询使用某些查找来执行过滤,而无需在管理界面中公开这些查找。

为此,您需要重写 ModelAdmin 上的 lookup_allowed() 方法。这是示例代码,详细信息请查看上面提到的帖子。

class DetailAdmin(admin.ModelAdmin):
    valid_lookups = ()
    def lookup_allowed(self, lookup, *args, **kwargs):
        if lookup.startswith(self.valid_lookups):
            return True
         return super(DetailAdmin, self).lookup_allowed(lookup, *args, **kwargs)

Prior to Django 1.2.4, you could create filtered links to models in the admin using GET query like this: http://your_site.com/admin/your_app/detail/?master__id__exact=2.

But it was a bit of security hole, and got fixed. Now you'll get a SuspiciousOperation exception if you try to filter your models using lookup that is not specified in list_filter attribute.

Though there's a fix for that. This workaround implements a valid_lookups attribute so that you can perform filtering using some lookups via URL get query, without having these lookups exposed in the admin interface.

For this to work, you'll need to override lookup_allowed() method on your ModelAdmin. Here is the example code, check the post mentioned above for details.

class DetailAdmin(admin.ModelAdmin):
    valid_lookups = ()
    def lookup_allowed(self, lookup, *args, **kwargs):
        if lookup.startswith(self.valid_lookups):
            return True
         return super(DetailAdmin, self).lookup_allowed(lookup, *args, **kwargs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文