2017-11-17 2 views
0

좋은 하루, ContentType 모델과 관련된 필드에 장고 자동 완성 3.2.10ForeignKey 관계가있는 장고 관리자입니다. 이름 위에 ContentType을 필터링해야하지만 실제로는 @property입니다. 이렇게 할 수있는 방법이 있습니까?django 자동 완성을 ContentType과 관련된 필드와 함께 사용하는 방법?

업데이트 : 나는 모델 'verbose_name 대신 name의 이상 ContentType를 필터링하는 데 실제로 필요합니다.

답변

0

답변을 찾았습니다.보기가 조금 엉망이지만 작동합니다.

class TransactionAutocomplete(autocomplete.Select2QuerySetView): 
    def get_queryset(self): 
     user = self.request.user 
     if user.is_authenticated() and user.is_staff: 
      if self.q: 
       models = apps.get_models() 
       result_list = [] 
       for model in models: 
        if self.q in model._meta.verbose_name: 
         result_list.append(
          ContentType.objects.get_for_model(model) 
         ) 
       return result_list 
      else: 
       return ContentType.objects.all() 
     else: 
      return ContentType.objects.none() 
관련 문제