나는 이런 일이 실현 가능하다고 생각하지만 보편적으로 (ORM에 대한 읽기 전용 액세스 만 허용) 보안적인 방식으로 작성하는 것은 매우 어려울 것입니다. 사용자 지정 관리자 클래스에 명시 적으로 표시 방법을 미리 정의 된 세트에
제한 조치 :
여기에 몇 가지 아이디어입니다. 예를 들어 :
from django.db import models
class MarkupAccessManager(models.Manager):
def count(self):
return super(MarkupAccessManager, self).count()
count.expose_to_markup = True
class AfricanSwallow(models.Model):
objects = MarkupAccessManager()
마크 업에서 모델을 참조하려면, 당신은 django.contrib.contenttypes
프레임 워크를 활용할 수 있으며, 태그의 형식은 다음과 수 : app_label.model_name action
또는 app_label.model_name action arg1 arg2
을.
선택한 마크 업 언어에 따라 사용자 정의 태그 (언어가 제공하는 경우), 장고 템플릿 태그 또는 일반 정규 표현식을 사용할 수 있습니다. 당신이 태그의 내용을 일단, 이것은 당신이 언급 된 방법의 출력을 대체 할 수있는 방법은 다음과 같습니다
from django.contrib.contenttypes.models import ContentType
def replace_tag(tag):
"""
'birds.africanswallow count' => birds.models.AfricanSwallow.objects.count()
"""
bits = tag.split()
model_ref = bits[0]
action = bits[1]
args = bits[2:]
try:
ct = ContentType.objects.get_by_natural_key(*model_ref.split('.'))
except ContentType.DoesNotExist:
return 'Invalid model reference.'
model = ct.model_class()
method = getattr(model._base_manager, action, None)
if not method or not method.expose_to_markup:
return 'Invalid action.'
return method(*args)
가 자동 완성을 제공하기 위해,이 라인을 따라 뭔가 당신이 사용할 수있는 모든 목록을 작성하는 데 도움이 될 것이다 옵션 :
from django.db.models.loading import get_models
from django.contrib.contenttypes.models import ContentType
def model_refs():
for model in get_models():
if isinstance(model._base_manager, MarkupAccessManager):
ct = ContentType.objects.get_for_model(model)
yield '%s.%s' % (ct.app_label, ct.model)
def actions():
for attr_name in dir(MarkupAccessManager):
attr = getattr(MarkupAccessManager, attr_name)
if attr.expose_to_markup:
yield attr.__name__
코드를 테스트하지 않았습니다. 희망이 조금 도움이됩니다.
코드 삽입 위험이 감지되었습니다. – Eduardo
그래, 유효한 코드로 신중하게 구문 분석 할 수있는 새로운 구문을 고안하지 않는 한 실제로 그렇게하고 싶지는 않습니다. 그렇지 않으면 해킹을 위해 스스로를 설정하고 있습니다. –
구문을 발명 할 필요가 없다고 생각합니다. – jMyles