본문 바로가기
파이썬

파이썬 Django, 모델 메서드에서 쿼리 필터링

by º기록 2020. 12. 18.
반응형

다음 모델이 있습니다.

def Foo(Models.model):
    size = models.IntegerField()
    # other fields

    def is_active(self):
         if check_condition:
              return True
         else:
              return False

def Bar(Models.model):
     foo = models.ForeignKey("Foo")
     # other fields

이제 활성 Foo가있는 Bar를 다음과 같이 쿼리하고 싶습니다.

Bar.objects.filter(foo.is_active())

다음과 같은 오류가 발생합니다.

SyntaxError at /
('non-keyword arg after keyword arg'

이것을 어떻게 달성 할 수 있습니까?

 

해결 방법

 

모델 메서드 또는 속성에 대해 쿼리 할 수 ​​없습니다. 쿼리에서 그 안에있는 기준을 사용하거나 목록 이해 또는 genex를 사용하여 Python에서 필터링하십시오.

 

참조 페이지 https://stackoverflow.com/questions/2276768

 

 

반응형

댓글