Django 管理站点:如何根据 verbose_name 更改管理 URL
Django admin site: how to change admin URL based on verbose_name
class Meta: verbose_name
仅更改其显示名称。但是当它被点击时。我希望它的 url 也根据冗长的名称进行更改
这是我的代码:
型号:
tables = [f.id for f in StockId.objects.raw(
"SELECT TABLE_NAME AS id FROM INFORMATION_SCHEMA.tables WHERE TABLE_NAME LIKE 'my_project_auth_stockid%%'")]
table_map = {}
for tbl in tables:
class Stocks(models.Model):
user_id = models.IntegerField()
product_name = models.CharField(max_length=100)
mrp = models.IntegerField()
selling_price = models.IntegerField()
class Meta:
db_table = tbl
managed = False
verbose_name = _(tbl)
verbose_name_plural = _(tbl)
table_map[tbl] = Stocks
Admin.py
from my_project_auth.models import *
class AllStocksAdmin(AdminSite):
site_header = 'All User Stocks'
stockadmin = AllStocksAdmin(name="stockadmin")
for tbl in tables:
class StocksAdmin(ImportExportModelAdmin):
resource_class = StocksAdminResource
list_display = ["product_name"]
stockadmin.register(table_map[tbl], StocksAdmin)
我想做的是在上面从单一模型中制作多个股票 table。但是每只股票的管理面板都不起作用。所有股票 table 都指向单一 table 因为管理员 URL 是基于模型的 class 名称。
我正尝试在 Admin.py 中做类似的事情,请提出更改建议:
for tbl in tables:
class StocksAdmin(ImportExportModelAdmin):
resource_class = StocksAdminResource
list_display = ["product_name"]
def get_urls(self): # Not Working
urls = super(StocksAdmin, self).get_urls()
my_urls = path(f'stockadmin/{tbl}/', self.admin_view(stockadmin))
return my_urls + urls
为了解决多个同名模型的问题,我们可以使用 type
的三参数形式动态生成具有唯一名称的模型
from django.db import models
tables = [f.id for f in StockId.objects.raw(
"SELECT TABLE_NAME AS id FROM INFORMATION_SCHEMA.tables WHERE TABLE_NAME LIKE 'my_project_auth_stockid%%'")]
class StockBase(models.Model):
user_id = models.IntegerField()
product_name = models.CharField(max_length=100)
mrp = models.IntegerField()
selling_price = models.IntegerField()
class Meta:
abstract = True
for tbl in tables:
class Meta:
db_table = tbl
managed = False
verbose_name = tbl
verbose_name_plural = tbl
locals()[f'Stocks{tbl}'] = type(f'Stocks{tbl}', (StockBase,), {'Meta': Meta, '__module__': StockBase.__module__})
这将导致多个模型具有相同字段,来自基础 class,并且所有模型都具有唯一名称。
我在管理中使用 __subclasses__
获取所有创建的模型,但它几乎相同,只需注册所有模型。
from django.contrib import admin
from . import models
class StocksAdmin(admin.ModelAdmin):
list_display = ["product_name"]
for cls in models.StockBase.__subclasses__():
admin.site.register(cls, StocksAdmin)
现在您的管理员应该充满 tens/hundreds/thousands 重复但命名略有不同的模型
class Meta: verbose_name
仅更改其显示名称。但是当它被点击时。我希望它的 url 也根据冗长的名称进行更改
这是我的代码:
型号:
tables = [f.id for f in StockId.objects.raw(
"SELECT TABLE_NAME AS id FROM INFORMATION_SCHEMA.tables WHERE TABLE_NAME LIKE 'my_project_auth_stockid%%'")]
table_map = {}
for tbl in tables:
class Stocks(models.Model):
user_id = models.IntegerField()
product_name = models.CharField(max_length=100)
mrp = models.IntegerField()
selling_price = models.IntegerField()
class Meta:
db_table = tbl
managed = False
verbose_name = _(tbl)
verbose_name_plural = _(tbl)
table_map[tbl] = Stocks
Admin.py
from my_project_auth.models import *
class AllStocksAdmin(AdminSite):
site_header = 'All User Stocks'
stockadmin = AllStocksAdmin(name="stockadmin")
for tbl in tables:
class StocksAdmin(ImportExportModelAdmin):
resource_class = StocksAdminResource
list_display = ["product_name"]
stockadmin.register(table_map[tbl], StocksAdmin)
我想做的是在上面从单一模型中制作多个股票 table。但是每只股票的管理面板都不起作用。所有股票 table 都指向单一 table 因为管理员 URL 是基于模型的 class 名称。
我正尝试在 Admin.py 中做类似的事情,请提出更改建议:
for tbl in tables:
class StocksAdmin(ImportExportModelAdmin):
resource_class = StocksAdminResource
list_display = ["product_name"]
def get_urls(self): # Not Working
urls = super(StocksAdmin, self).get_urls()
my_urls = path(f'stockadmin/{tbl}/', self.admin_view(stockadmin))
return my_urls + urls
为了解决多个同名模型的问题,我们可以使用 type
的三参数形式动态生成具有唯一名称的模型from django.db import models
tables = [f.id for f in StockId.objects.raw(
"SELECT TABLE_NAME AS id FROM INFORMATION_SCHEMA.tables WHERE TABLE_NAME LIKE 'my_project_auth_stockid%%'")]
class StockBase(models.Model):
user_id = models.IntegerField()
product_name = models.CharField(max_length=100)
mrp = models.IntegerField()
selling_price = models.IntegerField()
class Meta:
abstract = True
for tbl in tables:
class Meta:
db_table = tbl
managed = False
verbose_name = tbl
verbose_name_plural = tbl
locals()[f'Stocks{tbl}'] = type(f'Stocks{tbl}', (StockBase,), {'Meta': Meta, '__module__': StockBase.__module__})
这将导致多个模型具有相同字段,来自基础 class,并且所有模型都具有唯一名称。
我在管理中使用 __subclasses__
获取所有创建的模型,但它几乎相同,只需注册所有模型。
from django.contrib import admin
from . import models
class StocksAdmin(admin.ModelAdmin):
list_display = ["product_name"]
for cls in models.StockBase.__subclasses__():
admin.site.register(cls, StocksAdmin)
现在您的管理员应该充满 tens/hundreds/thousands 重复但命名略有不同的模型