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:*

   - Item 1) surface_color=[1, 2, 3]
   - Item 2) surface_color=[1, 2]
   - Item 3) surface_color=[2]
   - Item 4) surface_color=[1, 3]
   
*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:

   1. Item 4, since it has both 1 and 3 (2 matches)
   2. Item 1, since it has both 1 and 3 (2 matches)
   3. Item 2, since it has 1 (1 match)
   4. Item 3, since it has none (0 matches)
   
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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/008e95a6-a04e-4bb1-a8e2-e5ee5ea632d8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to