使用Djang通过模型字段定义ManyToManyField的顺序

2024-09-30 04:32:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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')

迪亚戈1.8


Tags: bymodelmodelspositionorderallproductclass

热门问题