class ProductRelation(models.Model):
product_a = models.ForeignKey('Product')
product_a_rank = models.PositiveSmallIntegerField('Position')
product_b = models.ForeignKey('Product')
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',
)
您好。我有product
产品实例。我怎样才能得到那些有序的结果?
product.b_products.all().order_by('product_b_rank')
和
product.a_products.all().order_by('product_a_rank')
Django 1.8
答案 0 :(得分:1)
使用双下划线到order_by
相关字段:
Product.objects.order_by("b_products__product_b_rank")
编辑:
product.product_a.order_by('product_b_rank')
product.product_b.order_by('product_a_rank')