class CDR(models.Model):
order = models.ForeignKey(Order)
call = models.ForeignKey(Call)
start_date = models.DateTimeField(auto_now_add=True)
end_date = models.DateTimeField(null=True)
remark = models.CharField(max_length=200, null=True)
class CDRAdmin(admin.ModelAdmin):
search_fields = ('order__id',)
raw_id_fields = ('call', 'order')
list_display = ('call', 'order', 'start_date', 'remark')
models = CDR
因为CDR表Call和Order中有2个外键,但是为了在list_display中显示这些id,它会与Call和Order表连接,这会导致查询速度变慢,因为所有这3个表都有大量的数据行。
有没有办法在没有加入的情况下解决它?
当我在list_display中使用call__id和order__id时,它会引发ImproperlyConfigured
而没有__id它会在结合结果时显示__repr__
对象。
答案 0 :(得分:3)
外键的ID可用作<name>_id
属性,因此您可以通过自定义方式显示ID:
class CDRAdmin(admin.ModelAdmin):
list_display = ('call_id_display', 'order_id_display', 'start_date', 'remark')
def call_id_display(self, obj):
return obj.call_id
call_id_display.short_description = 'Call ID'
def order_id_display(self, obj):
return obj.order_id
order_id_display.short_description = 'Order ID'