Define an order for ManyToManyField by through model field

17 views
Skip to first unread message

Dmitry Voronin

unread,
Nov 2, 2015, 5:38:47 PM11/2/15
to Django users
Question on Stackoverflow: http://stackoverflow.com/questions/33486705/define-an-order-for-manytomanyfield-by-through-model-field-with-django

    class ProductRelation(models.Model):
        product_a
= models.ForeignKey('Product', related_name='c+')
        product_a_rank
= models.PositiveSmallIntegerField('Position')
        product_b
= models.ForeignKey('Product', related_name='c+')
        product_b_rank
= models.PositiveSmallIntegerField('Postition')


   
class Product(models.Model):
        b_products
= models.ManyToManyField('self', through=ProductRelation, symmetrical=False,
        through_fields
=('product_a', 'product_b'),
        related_name
='a_products')


Hello. I have `product` Product instance. How can I get those ordered results?
  
product.b_products.all().order_by('product_b_rank')
and  
product.a_products.all().order_by('product_a_rank')

Django 1.8

Reply all
Reply to author
Forward
0 new messages