我有一个与Book有外键关系的Person模型.本书有很多领域,但我最关心的是"作者"(标准的CharField).
话虽如此,在我的PersonAdmin模型中,我想使用"list_display"显示"book.author".我已经尝试了所有这些明显的方法(见下文),但似乎没有任何效果.有什么建议?
class PersonAdmin(admin.ModelAdmin):
list_display = ['book.author',]
Run Code Online (Sandbox Code Playgroud) # admin.py
class CustomerAdmin(admin.ModelAdmin):
list_display = ('foo', 'number_of_orders')
# models.py
class Order(models.Model):
bar = models.CharField[...]
customer = models.ForeignKey(Customer)
class Customer(models.Model):
foo = models.CharField[...]
def number_of_orders(self):
return u'%s' % Order.objects.filter(customer=self).count()
Run Code Online (Sandbox Code Playgroud)
我如何根据客户的不同对客户进行排序number_of_orders?
admin_order_fieldproperty不能在这里使用,因为它需要一个数据库字段来进行排序.是否有可能,因为Django依赖底层数据库来执行排序?创建一个包含订单数量的聚合字段在这里看起来有点过分.
有趣的是:如果您在浏览器中手动更改URL以对此列进行排序 - 它按预期工作!
如何向django admin(显示在模型仪表板右侧的过滤器)添加自定义过滤器?我知道很容易包含一个基于该模型字段的过滤器,但是这样的"计算"字段呢:
class NewsItem(models.Model):
headline = models.CharField(max_length=4096, blank=False)
byline_1 = models.CharField(max_length=4096, blank=True)
dateline = models.DateTimeField(help_text=_("date/time that appears on article"))
body_copy = models.TextField(blank=False)
when_to_publish = models.DateTimeField(verbose_name="When to publish", blank=True, null=True)
# HOW CAN I HAVE "is_live" as part of the admin filter? It's a calculated state!!
def is_live(self):
if self.when_to_publish is not None:
if ( self.when_to_publish < datetime.now() ):
return """ <img alt="True" src="/media/img/admin/icon-yes.gif"/> """
else:
return """ <img alt="False" src="/media/img/admin/icon-no.gif"/> """
is_live.allow_tags = True
Run Code Online (Sandbox Code Playgroud)
class NewsItemAdmin(admin.ModelAdmin):
form = NewsItemAdminForm
list_display …Run Code Online (Sandbox Code Playgroud) 我有一个模型Data,与这样的表相关联(该模型Data仅由IntegerField组成):
subject | year | quarter | sales |
----------------------------------
1 | 2010 | 1 | 20 |
1 | 2010 | 2 | 100 |
1 | 2010 | 3 | 100 |
1 | 2010 | 4 | 20 |
1 | 2011 | 1 | 30 |
1 | 2011 | 2 | 50 |
1 | 2011 | 4 | 40 |
2 | 2010 | 1 | 30 |
2 | …Run Code Online (Sandbox Code Playgroud) 如何在 django admin 中对不存在的字段进行排序。基本上我有服务器和应用程序模型,第三个关系模型将它们链接起来。我看到了您的回复,但不确定将您提到的代码放在哪里。这是模型和admin.py
# Model.py File
class Server(models.Model):
name = models.CharField(max_length=100, unique=True)
operating_system = models.CharField(max_length=20, choices=constants.OPERATING_SYSTEMS.items())
@property
def number_of_apps(self):
return ServerApplicationRelationship.objects.filter(server=self).count()
class Application(models.Model):
name = models.CharField(max_length=100, unique=True)
hosted_on = models.ManyToManyField(Server, through='ServerApplicationRelationship', blank=True,)
@property
def number_of_servers(self):
return ServerApplicationRelationship.objects.filter(app=self).count()
# number_of_servers.server_field = 'server__count'
class ServerApplicationRelationship(models.Model):
server = models.ForeignKey(Server, blank=True, )
# server_tag = models.ForeignKey(Server, through_fields= 'tags')
app = models.ForeignKey(Application, blank=True)
# Admin.py file
@admin.register(Application)
class ApplicationAdmin(admin.ModelAdmin):
inlines = [ApplicationInLine]
list_display = ['name', 'number_of_servers']
list_display_links = ['name', 'number_of_servers']
ordering = ('number_of_servers', 'name') …Run Code Online (Sandbox Code Playgroud)