如何显示Django管理员更改列表外键子代的视图? [英] How Do I Show Django Admin Change List View of foreign key children?

查看:130
本文介绍了如何显示Django管理员更改列表外键子代的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用广告系列>类别>帐户模型的应用程式。理想情况下,我希望用户能够点击广告系列管理列表视图中的链接,并转到像/ admin / myapp / campaign / 2 / accounts /这样的网址,这将显示所有的django管理员视图方便的ChangeList设施,但被过滤以仅显示指定广告系列中类别的帐户(即Account.object.filter(category_ campaign _id = 2))。 (注意,类别本身我很高兴只是在这个帐户列表视图中的过滤器)。

I'm working on an app with a Model heirarchy of Campaign > Category > Account. Ideally I'd like to users to be able to click on a link in the campaign admin list view and go to a url like "/admin/myapp/campaign/2/accounts/" which will show a django admin view with all the handy ChangeList amenities but which is filtered to show just the accounts in categories in the specified campaign (ie. Account.object.filter(category_campaign_id = 2)). (Note, categories themselves I'm happy to just be "filters" on this accounts list view).

我似乎找不到任何参考的方式模仿这个在许多其他框架中常见的这个item-click-to-list-of-foriegn-key-children方法。

I can't seem to find any reference to a way to mimic this item-click-goes-to-list-of-foriegn-key-children approach that is common in many other frameworks.

可以吗?在django范式中是否有更好的方法?

Is it possible? Is there a "better" approach in the django paradigm?

感谢任何帮助!

推荐答案

这是一个有趣的问题,所以我鞭打了一个示例应用程序来计算出来。

This was an interesting question so I whipped up a sample app to figure it out.

# models.py
from django.db import models

class Campaign(models.Model):
    name = models.CharField(max_length=20)

    def __unicode__(self):
        return unicode(self.name)

class Category(models.Model):
    campaign = models.ForeignKey(Campaign)
    name = models.CharField(max_length=20)

    def __unicode__(self):
        return unicode(self.name)

class Account(models.Model):
    category = models.ForeignKey(Category)
    name = models.CharField(max_length=20)

    def __unicode__(self):
        return unicode(self.name)

# admin.py
from django.contrib import admin
from models import Campaign, Category, Account

class CampaignAdmin(admin.ModelAdmin):
    list_display = ('name', 'related_accounts', )

    def related_accounts(self, obj):
        from django.core import urlresolvers
        url = urlresolvers.reverse("admin:<yourapp>_account_changelist")
        lookup = u"category__campaign__exact"
        text = u"View Accounts"
        return u"<a href='%s?%s=%d'>%s</a>" % (url, lookup, obj.pk, text)
    related_accounts.allow_tags = True
admin.site.register(Campaign, CampaignAdmin)
admin.site.register(Category)

class AccountAdmin(admin.ModelAdmin):
    list_display = ('category', 'name')
    list_filter = ('category__campaign',)
admin.site.register(Account, AccountAdmin)

您需要替换为您的应用程序的名称,

You'll need to replace with the name of your app where the Account ModelAdmin lives.

注意:自从Django 1.2.4,Django 1.1.3和Django 1.3 beta 1以来,AccountAdmin上的list_filter是必需的,它通过URL参数引入了任意过滤的保护在管理员中。

Note: the list_filter on the AccountAdmin is required since Django 1.2.4, Django 1.1.3 and Django 1.3 beta 1, which introduced protection from arbitrary filtering via URL parameter in the admin.

这篇关于如何显示Django管理员更改列表外键子代的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆