django admin list_display - returning multiple column values with single callable (django 1.9, python=2.7.x)
<p>So my Django code for <code>admin.py</code> looks something like this:</p>

<pre><code>class MyUserAdmin(UserAdmin):
    form = MyUseChangeForm
    list_display = ('email', 'is_admin', 'is_superuser', 
                    'total_tasks', 'total_tests')
    list_filter = ('is_admin', 'is_superuser')
    ordering = ('email',)

    def total_tests(self, obj):
        others = OtherObject.objects.filter(user=obj)
        total = 0
        correct = 0
        for other in others:
            # ... etc
        return # ...
    total_tests.short_description = "Tests Taken"

    def total_tasks(self, obj):
        return OtherObject.objects.filter(user=obj).count()
    total_tasks.short_description = "Tasks Done"
</code></pre>

<p>However, realizing that every callable I make (<code>total_tasks</code>, <code>total_tests</code>) will make this page load suuuuuuper slow when I have more database entries - is there a way to share computation? Maybe cache so that every callable doesn't have to query ALL the items from different tables over and over again?</p>

<p>First of all tune tune your code.</p>

<ol>
<li><code>total_tasks</code> - should be <code>OtherObject.objects.filter(user_id=obj.pk).count()</code> - it will boost you a bit.</li>
<li>You've hidden the body of for loop in <code>total_tests</code>. I am afraid that you refer to some objects connected with <code>OtherObject</code> model. Without <a href="https://docs.djangoproject.com/en/1.10/ref/models/querysets/#select-related" rel="nofollow">select_related</a> you can almost DDOS your database and make the view extremaly slow.</li>
</ol>

python django