我有一个与用户有m2m关系的模型:
class SomeModel(models.Model):
user=models.ManyToManyField(User,related_name='linked',blank=True,null=True)
我有一个查询:
linked_objects=SomeModel.objects.filter(user__id__contains=request.user.id)
结果按用户ID排序。我想要做的是,通过User和SomeModel之间的关系表的主键对结果进行排序,这将是appname_somemodel_user。这可能吗?
答案 0 :(得分:1)
这可能是错的,但我认为如果你明确指定它,你只能参考中间表。
class SomeModel(models.Model):
user=models.ManyToManyField(User,related_name='linked',blank=True,null=True, through='SomeModelUser')
class SomeModelUser(models.Model):
user = models.ForeignKey(User)
some_model = models.ForeignKey(SomeModel)
linked_objects=SomeModel.objects.filter(user__id__contains=request.user.id).order_by('somemodeluser__pk')