2012-01-21 4 views
0

꽤 고급 파이썬 사용자 오전 django 채우는, 나는 아래에 채우려 고 노력하고 붙어 있지만 list_choices 잘못 처리 오전 생각합니다. 오류 아래 사전에

class LocationManager(TranslationManager): 
    def get_location_list(self, lang_code, site=None): 
     # this function is for building a list to be used in the posting process 
     # TODO: tune the query to hit database only once 
     list_choices = {} 
     for parents in self.language(lang_code).filter(country__site=site, parent=None):  
      list_child = ((child.id, child.name) for child in self.language(lang_code).filter(parent=parents)) 
      list_choices.setdefault(parents).append(list_child) 

     return list_choices 

는 두 번째 인수없이 setdefault을 사용하기 때문이다

>>> 
>>> Location.objects.get_location_list(lang_code='en', site=current_site) 
Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/home/mo/Projects/mazban/mazban/apps/listing/geo/models.py", line 108, in get_location_list 
    list_choices.setdefault(parents).append(list_child) 
AttributeError: 'NoneType' object has no attribute 'append' 
+0

문제가 무엇인가

이 고정 된 코드를 시도? – Marcin

+0

모델 코드를 알려주십시오. –

+0

list_choices가 항상 비어 있습니다. –

답변

1

을 얻고있다. 이 경우 None을 반환합니다.

# this is much more confinient and clearer 
from collections import defaultdict 

def get_location_list(self, lang_code, site=None): 
    # this function is for building a list to be used in the posting process 
    # TODO: tune the query to hit database only once 
    list_choices = defaultdict(list) 
    for parent in self.language(lang_code).filter(country__site=site, parent=None): 
     list_child = self.language(lang_code).filter(parent=parent).values_list('id', 'name') 
     list_choices[parent].extend(list_child) 

    return list_choices