tolomea/django-auto-prefetch

[Feature Request] More specific check for inheritance

dkgitdev opened this issue · 1 comments

Description

Currently, when user uses auto_prefetch.ForeignKey on the model that does not inherit auto_prefetch.Model, following message is being displayed upon trying to serialize the object using pickle:

Traceback (most recent call last):
  File "/home/dk/.pyenv/versions/p38/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3417, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-7969eb7b9b95>", line 4, in <module>
    caches['default'].set('asddsa', p, 60)
  File "/home/dk/.pyenv/versions/p38/lib/python3.8/site-packages/django_redis/cache.py", line 27, in _decorator
    return method(self, *args, **kwargs)
  File "/home/dk/.pyenv/versions/p38/lib/python3.8/site-packages/django_redis/cache.py", line 76, in set
    return self.client.set(*args, **kwargs)
  File "/home/dk/.pyenv/versions/p38/lib/python3.8/site-packages/django_redis/client/default.py", line 126, in set
    nvalue = self.encode(value)
  File "/home/dk/.pyenv/versions/p38/lib/python3.8/site-packages/django_redis/client/default.py", line 358, in encode
    value = self._serializer.dumps(value)
  File "/home/dk/.pyenv/versions/p38/lib/python3.8/site-packages/django_redis/serializers/pickle.py", line 21, in dumps
    return pickle.dumps(value, self._pickle_version)
AttributeError: Can't pickle local object 'WeakValueDictionary.__init__.<locals>.remove'

It's really misleading and non-helpful.

Can we do something about it?

Here's considerations i'd like to offer you:

  1. Note in the readme (cheapest)
  2. Additional check, like for the one you have for metaclasses

If you don't have any time for that, I may manage to do this myself (using the second option). Just say so :)

This could possibly done by using issubclass in an overridden contribute_to_class, which Django calls when the field is attached.

If you’d like to prepare a PR, that would be great. Please include tests and a changelog note.