Basically, I need to order, based on multiple values from a ManyToManyField.
So what I want to achieve is having the objects with the most questioned values on top, continuing with objects that have less of the questioned values. Continuing with objects that don't have any of these values.
The models:
class Color(models.Model):
name = models.CharField(max_length=200)
class Item(models.Model):
surface_color = models.ManyToManyField(Color)
The instances created which are based on the models above:
Now I need to order based multiple colors:
Fake query:
Item.objects.all().order_by(surface_color_id=[1, 3])The query should have the following results:
Is this possible with a single queryset? Or do I need to spam multiple queries for each combination?
The only things I found on the internet were about ordering multiple fields, this is about values.
Any help is appreciated.