Упорядочивание в админке django по связанному полю manytomany
Это мой models.py
:
class Person(models.Model):
surname = models.CharField(max_length=100, blank=True, null=True)
forename = models.CharField(max_length=100, blank=True, null=True)
class PersonRole(models.Model):
ROLE_CHOICES = [
("Principal investigator", "Principal investigator"),
[...]
]
project = models.ForeignKey('Project', on_delete=models.CASCADE)
person = models.ForeignKey(Person, on_delete=models.CASCADE)
person_role = models.CharField(choices=ROLE_CHOICES, max_length=30)
class Project(models.Model):
title = models.CharField(max_length=200)
person = models.ManyToManyField(Person, through=PersonRole)
@admin.display(description='Principal investigator')
def get_PI(self):
return [p for p in self.person.filter(personrole__person_role__contains="Principal investigator")]
Я хотел бы упорядочить колонку 'principal investigator' в бэкенде django Admin для страницы projects
. Как я могу это сделать? Вот моя попытка, которая не работает:
admin.py
class ProjectAdmin(ImportExportModelAdmin):
ordering = ('title','get_principal_investigator')
def get_principal_investigator(self, obj):
return self.get_PI
get_principal_investigator.admin_order_field = "get_PI"
get_principal_investigator.short_description = "Principal investigator"