Closed victor-baumann closed 8 years ago
I encountered the same issue and tracked it down to the same code. Although I don't seem to get an exception, I get None returned instead.
I don't like the solution proposed in #214 as it means putting tables specific attributes in the db model (If I understand it correctly).
For me, the solution proposed by @smolotov is a better solution, as no changes are required to code that uses django-tables2.
Here is a patch that seems to work. If this looks good, can submit a merge request.
--- /usr/lib/python3/dist-packages/django_tables2/utils.py 2014-02-02 09:07:17.000000000 +0000
+++ /usr/lib/python2.7/dist-packages/django_tables2/utils.py 2014-10-16 05:33:24.000000000 +0000
@@ -4,6 +4,7 @@
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.test.client import FakePayload
+from django.db.models.manager import BaseManager
from itertools import chain
import inspect
import six
@@ -377,7 +378,8 @@
if safe and getattr(current, 'alters_data', False):
raise ValueError('refusing to call %s() because `.alters_data = True`'
% repr(current))
- current = current()
+ if not isinstance(current, BaseManager):
+ current = current()
# important that we break in None case, or a relationship
# spanning across a null-key will raise an exception in the
# next iteration, instead of defaulting.
@brianmay No code change is necessary as django adds that attribute already, see https://github.com/django/django/blob/ed37f7e979186c99a6f351c289eb486461601d80/django/db/models/fields/related.py#L676 and https://github.com/django/django/blob/ed37f7e979186c99a6f351c289eb486461601d80/django/db/models/fields/related.py#L881
@mbertheau Thanks for the correction; contrary to what I said before, your patch is probably the better patch, and is more likely to catch other classes that have similar issues.
Agreed. I think @mbertheau is the better patch and more likely to catch other cases
We are experiencing this problem too. A patch/updated release on PyPI would be much appreciated.
+1
Apparently @mbertheau pull request #214 fixes the related fields access. Integrating that pull request or providing an updated version that fixes that issue would be of great use.
+1
+1
Hi guys,
I tried this fix, but it did not work for me - it still fails and shows me "--" in the table-data.
My code example is in https://github.com/bradleyayers/django-tables2/issues/256
This bug is really bugging us. Polite request to @bradleyayers to check #214 and se if a Django 1.7+ compatible version (for M2M) can be published to PyPI.
@bradleyayers Any ETA on this? Still looks broken to us.
Think somebody needs to look at @thebarty's code and try to work out why @mbertheau pull request #214 doesn't fix the problem.
...Assuming of course #256 is related to this bug.
Actually #256 looks different, it isn't a ManyToMany relationship.
Suspect the problem in #256 is that the column resolves to a RelatedManager, and Django Tables doesn't know how to deal with it. So you need to tell it how, by overriding the appropriate render function.
I am having a memory problem. I can't remember how to reproduce the ManyToMany issue. Can somebody please provide a sample db model and table that doesn't work unless #214 is applied? If so, I might try writing a proper test case.
Mental note for future: including test cases before you forget is important!
+1 for @mbertheau 's solution. It fixed the problem for me too.
I'm not maintaining django-tables2, but I'm happy to give admin to someone else that's interested in maintaining it.
@jieter This is the single most annoying thing in tables2 right now :-)
Copied the patch in #214 to a new branch (rebase-214), which passes the test suite.
@brianmay did you manage to create a proper test for this?
No, sorry, no test case for this. Basically I am no longer involved in the project that triggered the problem :-(
I tracked down the cause to utils.Accessor https://github.com/bradleyayers/django-tables2/blob/master/django_tables2/utils.py#L376-L380
The problem is, that managers are callable in Django 1.7 and will raise an error when called without arguments. https://github.com/django/django/blob/master/django/db/models/fields/related.py#L654 https://docs.djangoproject.com/en/dev/releases/1.7/#s-using-a-custom-manager-when-traversing-reverse-relations
I kinda fixed it for me with:
But I don't know if this is a good solution, since I don't have a very good understanding of the inner workings of tables2 nor django:)