-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Call to model "__str__" when rendering a table with custom column #511
Comments
@jxrossel Can you share a minimal example for the models/table to reproduce this? |
from django.db import models
import django_tables2 as dt
class Device(models.Model):
name = models.CharField( _('name'), max_length=200, unique=True )
def __str__(self):
print( '__str__ called' )
return self.name
class DeviceTable(dt.Table):
class Meta:
model = Device
orderable = False
fields = ['name']
edit = dt.Column( verbose_name=_('Edit'), orderable=False, empty_values=[] )
render_edit = lambda self: 'lala'
The problem occurs only if |
This is a failing test without relying on Stack trace leads to django-tables2/django_tables2/rows.py Line 234 in 8344601
|
The problem is: django-tables2/django_tables2/utils.py Line 333 in 8344601
six.text_type(self) in the default implementation.
While I think this should be fixed, I also think your model's |
* Added a test to check the number of times str(model) happens * Use mock.patch to make the test code a bit cleaner * Fix #511: do not use __repr__ in error message of Accessor.resolve()
Thanks! Well, in my case, the str is not really expensive, but it requires multiple joins that I didn't need for this particular table. Thanks again for your work on django-tables2! |
released 1.17.0 |
Hi,
When rendering a table associated with a model, the model "str" method is called for each row. In my case, this leads to many unnecessary queries (or the need to use "select_related" for undisplayed data). Can this be avoided ?
The text was updated successfully, but these errors were encountered: