clokep / django-querysetsequence

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

Retrieving related values doen't work after order_by() #104

Open Genarito opened 1 year ago

Genarito commented 1 year ago

First of all, thanks about this amazing lib! I've found a little issue where values() doesn't work after calling order_by().

Code to replicate the problem:

class Question(models.Model):
    name = models.CharField(max_length=350)

class Answer(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)

class Answer2(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)

query = QuerySetSequence(Answer.objects.all(), Answer2.objects.all())

query.values('question__name')  # This works
query.order_by('question__name').values('question__name')  # This does NOT work

The error thrown is the following:

File "/my_path/venv/lib/python3.10/site-packages/queryset_sequence/init.py", line 565, in iter self._fetch_all() File "/my_path/venv/lib/python3.10/site-packages/queryset_sequence/init.py", line 542, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "/my_path/venv/lib/python3.10/site-packages/queryset_sequence/init.py", line 367, in iter yield from super().iter() File "/my_path/venv/lib/python3.10/site-packages/queryset_sequence/init.py", line 210, in _ordered_iterator iterables = sorted(iterables, key=comparator) File "/my_path/venv/lib/python3.10/site-packages/queryset_sequence/init.py", line 194, in comparator return _comparator(tuple_1[2], tuple_2[2]) File "/my_path/venv/lib/python3.10/site-packages/queryset_sequence/init.py", line 143, in comparator v1 = cls._get_fields(i1, field_names) File "/my_path/venv/lib/python3.10/site-packages/queryset_sequence/init.py", line 379, in _get_fields return itemgetter(field_names)(obj) KeyError: 'question.name'

clokep commented 1 year ago

Thanks for providing a simplified way to reproduce. Just to double check what version are you using? Does this work in any version?

Genarito commented 1 year ago

Hi @clokep ! I'm using the 0.17 version. I didn't try any other version

clokep commented 1 year ago

Thanks for confirming! I'm not sure if I'll have time to take a look at this soon, unfortunately.