clokep / django-querysetsequence

Chain multiple (disparate) QuerySets in Django
https://django-querysetsequence.readthedocs.io/
ISC License
107 stars 25 forks source link

'QuerySetSequence' object has no attribute '_cloning' #34

Closed vkosh closed 6 years ago

vkosh commented 6 years ago

I'm getting an exception when using QuerySetSequence with django-cacheops:

>>> from models import TestModel
>>> from queryset_sequence import QuerySetSequence
>>> q = QuerySetSequence(TestModel.objects.all())                                                                                                                                                                                                                  
>>> q.all()                                                                                                                                                                                                                                                            
Traceback (most recent call last):                                                                                                                                                                                                                                     
  File "<console>", line 1, in <module>                                                                                                                                                                                                                                
  File "/usr/local/lib/python3.5/site-packages/django/db/models/query.py", line 777, in all                                                                                                                                                                            
    return self._clone()                                                                                                                                                                                                                                               
  File "/usr/local/lib/python3.5/site-packages/cacheops/query.py", line 227, in _clone                                                                                                                                                                                 
    if self._cloning:                                                                                                                                                                                                                                                  
  File "/usr/local/lib/python3.5/site-packages/queryset_sequence/_inheritance.py", line 71, in __getattribute__                                                                                                                                                        
    (name, attr))                                                                                                                                                                                                                                                      
AttributeError: 'QuerySetSequence' object has no attribute '_cloning'                                                                                                                                                                                                  

Cacheops monkeypatches QuerySet and adds some new properties and methods to it: https://github.com/Suor/django-cacheops/blob/master/cacheops/query.py#L542

What I'm trying to achieve is autocomplete field for GenericForeignKey using django-autocomplete-light (https://django-autocomplete-light.readthedocs.io/en/master/gfk.html), but with caching.

I have tried to add '_cloning' and other patched fields to QuerySetSequence's descendant INHERITED_ATTRS, but with no result.

Could you propose a workaround for it?

clokep commented 6 years ago

👎 for monkey patching, but that doesn't fix your problem.

I'd expect that adding them to INHERITED_ATTRS would work, how are you attempting to modify that? Because of the implementation it pulls this property at creation time and uses it inside of a scoped function (see https://github.com/percipient/django-querysetsequence/blob/0c5d4db627e087de2020654e13cf239c11bf3f20/queryset_sequence/_inheritance.py#L29-L30 and https://github.com/percipient/django-querysetsequence/blob/0c5d4db627e087de2020654e13cf239c11bf3f20/queryset_sequence/_inheritance.py#L54-L72), which might make it hard to modify. I hadn't really considered something wanting to modify those.

vkosh commented 6 years ago

Thanks for the explanations. If I understand right, the inheritance dosn't help me with modifying INHERITED_ATTRS. I tried to modify them inplace, but there are too many other methods and properties to add to QuerySet and QuerySetSequence. I created an issue in cacheops: https://github.com/Suor/django-cacheops/issues/259

clokep commented 6 years ago

Left a comment in the other issue about what you might want to do here...