简体   繁体   English

为现有应用定制 django 管理页面的最佳方法

[英]Best approach to customize django admin page for existing app

I'm currently learning some basics of django by trying to implement admin page for the following app: PowerGSLB我目前正在通过尝试为以下应用程序实现管理页面来学习 django 的一些基础知识: PowerGSLB

Though it already has nice UI based on W2UI my goal is to learn django and make role based authentication with help of django admin module.虽然它已经有了基于 W2UI 的漂亮 UI,但我的目标是学习 django 并在 django 管理模块的帮助下进行基于角色的身份验证。 But it's actually not the point of the question.但这实际上不是问题的重点。

I got stuck with presenting basic list of DNS records to user.我一直坚持向用户展示 DNS 记录的基本列表。 DB model looks like this: DB model 看起来像这样:

db_model_image db_model_image

So, I ran through inspectdb and created models, based on this structure.因此,我运行了inspectdb并基于此结构创建了模型。 After all, my Records model looks like this:毕竟,我的记录model 看起来像这样:

class Records(models.Model):
    id = models.BigAutoField(primary_key=True)
    name_type = models.ForeignKey(NamesTypes, models.DO_NOTHING)
    content_monitor = models.ForeignKey(ContentsMonitors, models.DO_NOTHING)
    view = models.ForeignKey('Views', models.DO_NOTHING)
    disabled = models.BigIntegerField()
    fallback = models.BigIntegerField()
    weight = models.BigIntegerField()

    def __str__(self):
        return f"{self.name_type} {self.content_monitor} {self.disabled} {self.fallback} {self.weight}"

    def get_all(self, in_var):
        self.result = dict()

        # Objects to get results from
        self.name_type_obj = NamesTypes.objects.get(id=self.name_type_id)
        self.content_monitor_obj = ContentsMonitors.objects.get(id=self.content_monitor_id)
        self.view_obj = Views.objects.get(id=self.view_id)
        self.names_obj = Names.objects.get(id=self.name_type_obj.name_id)
        self.domain_obj = Domains.objects.get(id=self.names_obj.domain_id)
        self.contents_obj = Contents.objects.get(id=self.content_monitor_obj.content_id)
        self.monitor_obj = Monitors.objects.get(id=self.content_monitor_obj.monitor_id)
        self.types_obj = Types.objects.get(value=self.name_type_obj.type_value_id)

        # Result vars
        self.result['domain'] = self.domain_obj.domain
        self.result['name'] = self.names_obj.name
        self.result['type'] = self.types_obj.type
        self.result['content'] = self.contents_obj.content
        self.result['ttl'] = self.name_type_obj.ttl
        self.result['id'] = self.id
        self.result['disabled'] = self.disabled
        self.result['fallback'] = self.fallback
        self.result['persistence'] = self.name_type_obj.persistence
        self.result['weight'] = self.weight
        self.result['monitor'] = self.monitor_obj.monitor
        self.result['view'] = self.view_obj.rule

        return self.result[in_var]

And my admin.py for my app looks like this:我的应用程序的 admin.py 如下所示:

from django.contrib import admin

from .models import Records


@admin.register(Records)
class RecordsAdmin(admin.ModelAdmin):
    list_display = (
        'get_domain',
        'get_name',
        'get_type',
        'get_content',
        'get_ttl',
        'get_disabled',
        'get_fallback',
        'get_persistence',
        'get_weight',
        'get_monitor',
        'get_view',
    )

    def get_domain(self, obj):
        return obj.get_all('domain')

    def get_name(self, obj):
        return obj.get_all('name')

    def get_type(self, obj):
        return obj.get_all('type')

    def get_content(self, obj):
        return obj.get_all('content')

    def get_ttl(self, obj):
        return obj.get_all('ttl')

    def get_disabled(self, obj):
        return obj.get_all('disabled')

    def get_fallback(self, obj):
        return obj.get_all('fallback')

    def get_persistence(self, obj):
        return obj.get_all('persistence')

    def get_weight(self, obj):
        return obj.get_all('weight')

    def get_monitor(self, obj):
        return obj.get_all('monitor')

    def get_view(self, obj):
        return obj.get_all('view')

    get_domain.short_description = "Domain"
    get_name.short_description = "Name"
    get_type.short_description = "Type"
    get_content.short_description = "Content"
    get_ttl.short_description = "TTL"
    get_disabled.short_description = "Disabled"
    get_fallback.short_description = "Fallback"
    get_persistence.short_description = "Persistence"
    get_weight.short_description = "Weight"
    get_monitor.short_description = "Monitor"
    get_view.short_description = "View"

It's actually working (here is an image gslb_example ) but it's very slow, compare to original UI.它实际上可以工作(这是一个图像gslb_example ),但与原始 UI 相比,它非常慢。 Obviously this is because I have a lot of queries to database, instead of one big query, and here is the question - what is the best approach to do this in django admin?显然这是因为我对数据库有很多查询,而不是一个大查询,这是一个问题 - 在 django 管理员中执行此操作的最佳方法是什么?

I am using django v.3.1.4我正在使用 django v.3.1.4

Maybe you can modify the get_all(self, in_var) method也许你可以修改 get_all(self, in_var) 方法

and could do like this并且可以这样做

def get_all(self, in_var):
    self.result = dict()
    if in_var == "domain":
        return Domains.objects.get(id=self.names_obj.domain_id)
        

you can map in_var to model classes if you want and same you can do with other variables如果你愿意,你可以 map in_var 到 model 类,同样你可以使用其他变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM