[Django]-Django concatenate a db object with empty QuerySet

8๐Ÿ‘

โœ…

>>> empty = Person.objects.none()

if you use get you return a db object and get this error when you try use | to append the object to the empty qs:

>>> qs = empty|Person.objects.get(pk=1)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/dev/.virtualenvs/dev/lib/python2.7/site-packages/django/db/models/query.py", line 1018, in __or__
    return other._clone()
AttributeError: 'Person' object has no attribute '_clone'

however you can use the | operator to combine two query sets. To get the object as a query set we can use .filter():

>>> qs = empty|Person.objects.filter(pk=1)
>>> print qs
[<Person: A>]
>>> qs = qs|Person.objects.filter(pk=2)
>>> print qs
[<Person: A>, <Person: B>]
>>> 

Leave a comment