跳转到主要内容

在Django管理界面中添加了对通用关系的支持。

项目描述

一个简单的Django应用程序,使通用模型的查找更加容易。

安装

要安装,请将其添加到您的 INSTALLED_APPS 设置中。由于 django-genericadmin 没有任何模型,因此无需运行 manage.py syncdb

INSTALLED_APPS = (
   ...
   'genericadmin',
   ...
)

如果您正在使用staticfiles应用程序,则运行 manage.py collectstatic 并应正常。

如果您不知道我在说什么或您的Django版本小于1.3,则应将 genericadmin/media/js/ 链接或复制到您的资产目录,并将 GENERICADMIN_JS 设置为您刚复制文件的相对位置。

用法

要使用 django-genericadmin,您的模型管理类必须继承自 GenericAdminModelAdmin

所以一个模型管理类像

class NavBarEntryAdmin(admin.ModelAdmin):
    pass

admin.site.register(NavBarEntry, NavBarEntryAdmin)

变为

from genericadmin.admin import GenericAdminModelAdmin

class NavBarEntryAdmin(GenericAdminModelAdmin):
    pass

admin.site.register(NavBarEntry, NavBarEntryAdmin)

就这样。

提供的admin类

以下是 django-genericadmin 提供的admin类及其用途的简要概述。

  • GenericAdminModelAdmin — 具有至少一个通用外键的标准Django模型的管理器。

  • TabularInlineWithGenericStackedInlineWithGeneric — 具有通用关系并在线编辑的模型的普通内联管理器。

  • GenericTabularInlineGenericStackedInline — 用于提供 真正的多态关系(见下文)以及在管理后台中的通用关系。有关更多信息,请参阅Django文档此处

内联使用

要使用 django-genericadmin 与管理后台内联,您的模型必须继承自上述描述的 GenericAdminModelAdmin

from genericadmin.admin import GenericAdminModelAdmin

class NavBarEntryAdmin(GenericAdminModelAdmin):
    pass

admin.site.register(NavBarEntry, NavBarEntryAdmin)

此外,内联类必须继承自 StackedInlineWithGenericTabularInlineWithGeneric

from genericadmin.admin import GenericAdminModelAdmin, TabularInlineWithGeneric

class PagesInline(TabularInlineWithGeneric):
    model = ...

class NavBarEntryAdmin(GenericAdminModelAdmin):
    inlines = [PagesInline, ]

...

请注意,您不能混合使用。如果您要使用通用内联,使用该内联的类必须继承自 GenericAdminModelAdmin

指定处理的字段

在大多数情况下,django-genericadmin 会正确地确定您的模型上哪些字段是通用外键,并执行正确的操作。如果您想自己指定字段(控制自己的命运等等),您可以在管理类上使用 generic_fk_fields 属性。请注意,您可以指定每个管理类上内联管理器的字段。因此,对于上述提到的内联管理器,您会这样做

class PagesInline(TabularInlineWithGeneric):
    model = AReallyCoolPage
    generic_fk_fields = [{
        'ct_field': <field_name_for_contenttype_fk>,
        'fk_field': <field_name_for_object_id>,
    }]

如果您想使用多个字段对,只需将更多字典添加到列表中即可。

如果您使用 ct_fieldct_fk_field 属性,django-genericadmin 总是会忽略这些字段,甚至不会尝试使用它们。

禁止内容类型

可以从内容类型选择列表中删除特定的内容类型。例如

class NavBarEntryAdmin(GenericAdminModelAdmin):
    content_type_blacklist = ('auth/group', 'auth/user', )

白名单内容类型

可以从内容类型选择列表中显示的特定内容类型。例如

class NavBarEntryAdmin(GenericAdminModelAdmin):
    content_type_whitelist = ('auth/message', )

请注意,这只发生在客户端;在模型级别上没有对黑名单的强制执行。

按内容类型查找参数

为每个内容类型提供额外的查找参数,类似于limit_choices_to如何与原始id字段一起使用。例如

class NavBarEntryAdmin(GenericAdminModelAdmin):
    content_type_lookups = {'app.model': {'field': 'value'}

真正的多态关系

django-genericadmin 还提供了一个用户界面,可以轻松管理一个特别有用的模型,当将其作为其他模型的内联时,它可以从任何模型的任何条目到任何其他模型的任何条目建立关系。由于它具有双向的通用关系,这意味着它可以作为内联附加到 任何模型,而无需为要在其上使用它的每个模型创建唯一的外键。

这是一个多态模型的示例

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class RelatedContent(models.Model):
    """
    Relates any one entry to another entry irrespective of their individual models.
    """
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    parent_content_type = models.ForeignKey(ContentType, related_name="parent_test_link")
    parent_object_id = models.PositiveIntegerField()
    parent_content_object = generic.GenericForeignKey('parent_content_type', 'parent_object_id')

    def __unicode__(self):
        return "%s: %s" % (self.content_type.name, self.content_object)

以下是您如何设置您的 admin.py

from whateverapp.models import RelatedContent
from genericadmin.admin import GenericAdminModelAdmin, GenericTabularInline

class RelatedContentInline(GenericTabularInline):
    model = RelatedContent
    ct_field = 'parent_content_type' # See below (1).
    ct_fk_field = 'parent_object_id' # See below (1).

class WhateverModelAdmin(GenericAdminModelAdmin): # Super important! See below (2).
    content_type_whitelist = ('app/model', 'app2/model2' ) # Add white/black lists on this class
    inlines = [RelatedContentInline,]

(1) 默认情况下,ct_fieldct_fk_field 将默认为 content_typeobject_idct_fieldct_fk_field 用于从内联创建父链接到您附加到的模型(类似于Django如何使用更传统内联的外键执行此附加)。您也可以省略此配置,但如果您这样做,我鼓励您将模型属性从 parent_content_type & parent_object_id 更改为 child_content_type & child_object_id。我这样说是因为,当进行查询时,您会想知道您正在“遍历”哪个方向。

(2) 确保利用这些内联的任何管理类都是来自 django-genericadminGenericAdminModelAdmin 的子类,否则,使用javascript的便捷界面不会按预期工作。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

django-genericadmin-0.7.0.tar.gz (11.3 kB 查看哈希值)

上传时间

构建分发

django_genericadmin-0.7.0-py2.py3-none-any.whl (13.0 kB 查看哈希值)

上传时间 Python 2 Python 3