T
T
TitanFighter2017-01-24 01:50:50
Django
TitanFighter, 2017-01-24 01:50:50

How to sort in DB by number of objects in ListField in Django Rest Framework + Mongoengine?

There is the following model:

class Departments(Document):
        _id = fields.ObjectIdField()
        name = fields.StringField(blank=True, null=True)
        department_id = fields.StringField(blank=True, null=True)  # Added
        list_of_users = fields.ListField(blank=True, null=True)
        list_of_workstations = fields.ListField(blank=True, null=True)

As you can see, list_of_users and list_of_workstations are lists of objects.
I wrote a code in Python that pulls all the data from the database, puts it in a dictionary, and sorts it, but this method is too slow.
How can I sort Departments directly in the database by the number of list_of_users or list_of_workstations or by the ratio of list_of_users/list_of_workstations, something like:
departments = DepartmentStats.objects.order_by(len(list_of_users)).dsc

or
departments = DepartmentStats.objects.order_by(len(list_of_users)/len(list_of_workstations)).dsc

?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vlad_Fedorenko, 2017-01-24
@TitanFighter

stackoverflow.com/questions/28950366/order-by-length...

A
Anatoly Medvedev, 2017-08-06
@balamyt92

$mailer->IsHTML(true);

A
Arris, 2017-08-06
@Arris

PHP_EOL.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question