다음 필드가 있습니다
target_contenttype = models.ForeignKey(ContentType,
blank=True,
null=True,
related_name="target_object",
on_delete=models.PROTECT,
limit_choices_to={'model__in':(
''
)})
limit_choices_to에서 다른 앱에있는 관련 모델을 제한하는 방법에 대한 설명서를 찾을 수 없습니다. 누군가 도울 수 있습니까?
할 수 있습니다 중 하나를 사용 get_for_model()
(당신이 관련 모델을 가져온 경우) 또는 get_by_natural_key()
그것을 응용 프로그램 이름과 모델명을 전달, 모두 소문자로 :
from relatedapp.models import RelatedModel
limit_choices_to={'model__in':(
ContentType.objects.get_for_model(RelatedModel),
ContentType.objects.get_by_natural_key('relatedapp', 'relatedmodel'),
)}
또 다른 방법은 Q 개체를 만들어 앱 레이블 + 모델을 모두 소문자로 필터링하는 것입니다.
limit_choices_to=(
Q(app_label='app1', model='model1') |
Q(app_label='app2', model='model2')
)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다