2013-10-30 2 views
0

사용되지 않는 개체/선택되지 않은 개체를 새로 만들려고합니다. 사용 된 개체와 사용되지 않은 개체를 템플릿에 표시 할 수 있습니다.목록에 개체 제외

내 모델 :

class Benefit(models.Model): 
    name = models.CharField(max_length=200) 

class Profile(models.Model): 
    benefits = models.ManyToManyField(Benefit, blank=True, null=True, related_name="used_benefit") 

내보기 : 'ReverseManyRelatedObjectsDescriptor' object has no attribute 'all'

그때 all없이 시도했지만 한 :

class Profile(TemplateView): 
    template_name = "profile/benefits.html" 

    def get_context_data(self, **kwargs): 
      context = super(Profile, self).get_context_data(**kwargs) 
      context['unused_benefits'] = Profile.objects.exclude(pk__in=Profile.benefits.all()) 
      return context 

그것은 내가이 오류가 있기 때문에, 임 못하고 뭔가 오류가 발생합니다. 'ReverseManyRelatedObjectsDescriptor' object is not itterable

누구가 잘못된 일을하는지 알아?

+0

귀하의 질문은 나에게 분명하지 않다 : 당신이 그들에 첨부 된 프로필이없는 장점의 목록을 단순히 시도하는 경우

는, 당신은 실제로 프로필, 혜택 모델을하지 쿼리해야합니다. 사용하지 않거나 선택하지 않은 물체는 무엇을 의미합니까? –

+0

혜택이없는 곳에서 Profile 개체를 가져 보았습니까? 일치하는 것을 제외하는 대신에? – mconlin

답변

0

당신이하고있는 일은 전혀 이해가되지 않습니다. 프로필 클래스 자체에서는 benefits.all()에 액세스 할 수 없으며 해당 인스턴스에서만 액세스 할 수 있습니다. Profile.benefits.all()은 무엇을 의미할까요?

그게 효과가있다하더라도, 그것은 당신에게 혜택 목록을 줄 것입니다. 그런 다음 어떻게 그것을 사용하여 프로필을 pk에서 제외시킬 수 있습니까? 그런 다음 세 번째로 프로필 쿼리를 사용하면 사용되지 않는 혜택 목록을 얻을 수 있습니다.

unused_benefits = Benefits.objects.filter(profile__isnull=True) 
+0

다음과 같이 변경했습니다 :'context [ 'unused_benefits'] = Benefit.objects.filter (used_benefit__isnull = True)' –