在django admin中添加过滤器

时间:2011-05-30 09:54:45

标签: django django-admin

我有以下型号 -

class NetworkAdministrator (models.Model):
    email = models.EmailField()
    admin_name = models.CharField(max_length=100, blank=True)

class Network(models.Model):
    network = models.CharField(max_length=50)
    network_name = models.CharField(max_length=50)
    administrators = models.ManyToManyField(NetworkAdministrator)

在管理面板中,对于我添加的任何网络,它会显示所有管理员,就像它正在执行的操作一样 - NetworkAdministrator.objects.all()

我如何为每个特定网络做到这一点,管理员组只包括来自当前网络的那些 -

Network.objects.get(network='**network name**').administrators.all()

从功能上讲,这是我希望在管理面板中能够做到的事情:

1) add network = 'China'
2) add 2 administrators to this network = cadmin1, cadmin2, cadmin3
3) add network = 'India'
4) add 2 administrators to this network: iadmin1, iadmin2 
   (no administrators should show from the China network).

谢谢。

1 个答案:

答案 0 :(得分:0)

使用InlineModelAdmin

class AdministrationInline(admin.TabularInline):
    model = Network.administrators.through

class NetworkAdministratorAdmin(admin.TabularInline):
    inlines = [
        AdministrationInline,
    ]

class NetworkAdmin(admin.ModelAdmin):
    inlines = [
        AdministrationInline,
    ]
    exclude = ('administrators',)

admin.site.register(Network, NetworkAdmin)
admin.site.register(NetworkAdministrator, NetworkAdministratorAdmin)

请注意through中间模型和手册排除'administrators'