2012-08-27 68 views
0

我想在Django Admin中執行自定義過濾器,但似乎當我過濾基於父母和老師在我的情況下,我傾向於獲得重複的值。這意味着父母的數量(481)超過總數(221)。一些價值重複..我如何避免這種情況?任何想法?在Django的自定義列表過濾中重複的結果管理

admin.py:

class AdultRoleListFilter(SimpleListFilter): 

    title = _('Role of Adult') 
    parameter_name = 'adultrole' 
    def lookups(self, request, model_admin): 
     return (
      ('T', _('Teacher')), 
      ('P', _('Parent')),) 
    def queryset(self, request, queryset): 
     if self.value() == 'P': 
      return queryset.filter(relationships__role='P') 
     if self.value() == 'T': 
      return queryset.filter(relationships__role='T') 

Models.py

class Student(models.Model): 
    name = models.CharField(max_length=255) 
    birthday= models.DateField(blank=True,null=True) 

class Adult(models.Model): 
    user = models.OneToOneField(User) 
    students = models.ManyToManyField(Student, through='StudentAdultRelationship', related_name='adults') 

class class StudentAdultRelationship(models.Model): 

    PARENT = 'P' 
    TEACHER ='T' 
    FOLLOWER = 'F' 

    ROLE_CHOICES = (
     (PARENT, 'Parent'), 
     (TEACHER, 'Teacher'), 
     (FOLLOWER, 'Follower'), 
    ) 

    adult = models.ForeignKey(Adult, related_name='relationships') 
    student = models.ForeignKey(Student, related_name='relationships') 
    role = models.CharField(choices=ROLE_CHOICES, max_length=1) 

需要一些這方面的指導....

回答