I have a few objects in the database: object 1, object 2, object 3, .., object n
Now I am doing filter like this
MyDocument.search().filter("match", is_active=True).sort('id').execute()
Output:
searchDocumentobject 1, searchDocumentobject 2,searchDocumentobject 3, ....
Now I need to searchDocumentobject 2 in last from the list.
Need the output like this:
searchDocumentobject 1,searchDocumentobject 3, .... , searchDocumentobject 2
Thank you
In MyModel, add a new method, which returns 0 if you want to keep that document at last, else it'll return 1.
class MyModel(models.Model):
# Add new method here
def get_rank(self):
if self.id == 2: # your condition here
return 0 # return 0, if you want to keep it at last
return 1
Now, you can utilize this method in MyDocument. Add a new field in MyDocument, which we'll use for sorting.
class MyDocument(Document):
# Add new field here
rank = fields.IntegerField(attr='get_rank')
Now, you can query like this,
MyDocument.search().filter("match", is_active=True).sort('-rank', 'id').execute()