2015-02-01 2 views
0

내 웹 애플리케이션의 프론트 엔드에서 Annotator.JS (http://annotatorjs.org/)의 사용을 지원하기 위해 Django Rest Framework를 사용하고 있습니다. 문제는 Annotator.JS가 AJAX 요청의 프런트 엔드에서 보내는 JSON과 사용자가 만드는 주석을 저장하는 데 사용하는 모델입니다.DRF의 시리얼 라이저와 모델 디자인 사이의 변환

Annotator.JS 'JSON의 구조는 다음과 같습니다

{ 
    "id": "39fc339cf058bd22176771b3e3187329", # unique id (added by backend) 
    "annotator_schema_version": "v1.0",  # schema version: default v1.0 
    "created": "2011-05-24T18:52:08.036814", # created datetime in iso8601 format (added by backend) 
    "updated": "2011-05-26T12:17:05.012544", # updated datetime in iso8601 format (added by backend) 
    "text": "A note I wrote",     # content of annotation 
    "quote": "the text that was annotated", # the annotated text (added by frontend) 
    "uri": "http://example.com",    # URI of annotated document (added by frontend) 
    "ranges": [        # list of ranges covered by annotation (usually only one entry) 
    { 
     "start": "/p[69]/span/span",   # (relative) XPath to start element 
     "end": "/p[70]/span/span",    # (relative) XPath to end element 
     "startOffset": 0,      # character offset within start element 
     "endOffset": 120      # character offset within end element 
    } 
    ], 
    "user": "alice",       # user id of annotation owner (can also be an object with an 'id' property) 
    "consumer": "annotateit",     # consumer key of backend 
    "tags": [ "review", "error" ],    # list of tags (from Tags plugin) 
} 

내 주석 모델의 구조는 다음과 같습니다 I가 모델 구조에서 변환 할 수있는 시리얼을 만드는 방법을

class Annotation(models.Model): 
    datapoint = models.ForeignKey('datapoint.Datapoint', related_name='%(class)s_parent_datapoint_relation') 
    owner = models.ForeignKey('users.User', related_name='%(class)s_creator_relation') 

    # Key fields from the Annotator JSON Format: http://docs.annotatorjs.org/en/latest/annotation-format.html 
    annotator_schema_version = models.CharField(max_length=8, blank=True) 
    text = models.TextField(blank=True) 
    quote = models.TextField() 
    uri = models.URLField(blank=True) 
    range_start = models.CharField(max_length=50, blank=True) 
    range_end = models.CharField(max_length=50, blank=True) 
    range_startOffset = models.BigIntegerField() 
    range_endOffset = models.BigIntegerField() 
    tags = TaggableManager(blank=True) 

JSON?

P. Annotator.JS를 사용하면 위에서 언급 한 JSON 구조를 사용하여 추가 정보를 보낼 수 있으므로 Datapoint가 JSON 구조에 포함되어 있지 않다는 사실은 문제가되지 않습니다. 이것은 문제없이 전달 될 수 있습니다. 소유자는 JSON에서 User와 같을 것입니다.

어떤 도움을 주셔서 감사합니다.

답변

0

그냥 기본 ModelSerializer를 사용하면 무료 (버전, 텍스트, 인용구, URI)에 대한 모든 간단한 필드에 대한 직렬화를 가져야합니다. 직렬화 할 필드를 지정하면됩니다. 다른 필드는 간단뿐만 아니라 찾습니다

  • 이 범위는 당신이 사용자 정의 직렬화 메소드를 정의 할 수있는 SerializerMethodField을 사용할 수 있습니다, 객체 작성하려면합니다 (range_에서 값을 포함하는 사전의 배열을 반환 *이 모델에 속성) . JSON을 모델에 역 직렬화 할 수 있어야하는 경우 사용자 정의 필드를 정의해야합니다. > USER_ID SlugRelatedField 또한 TaggableManager에서 이동 SlugRelatedField을 사용할 수 있습니다
  • (당신이 사용자 개체에서 원하는 사용자 ID에 액세스 할 수 있습니다 가정) - -> [ "태그", "값"] (
  • 소유자에서 이동하려면 .. 다시, 당신은 TaggableManager에 의해 관리 모델 객체를 통해 원하는 값에 액세스 할 수 있습니다 가정 일반적으로

을, 당신이 원하는 모든 문서에 꽤 좋은 자세히 설명되어

+0

를 해결하기 위해 범위는 객체 : JSON을 deserialize 할 필요가 있으므로 사용자 정의 필드도 필요합니다. 범위 배열의 각 부분에 대해 하나입니까? 다른 범위 필드에서 언급 된 n? – allyjweir

+0

나는 당신이 원하는 필드를 가지고 직렬화 (Annotation-> Range 객체 json)를 처리 할 수있는 serializer를 생성하는 Range 객체 (보통 Python 클래스, Django 모델이 아닌)를 정의하는 것이 더 좋을 것이라고 생각한다. deserialization (Range 객체 json -> Range 객체) 및 원하는 모든 유효성 검사. 이것을 Annotation 모델 시리얼 라이저 (중첩 시리얼 라이저)의 필드로 포함하십시오. 그런 다음 Annotation serializer의 save() 메서드를 재정의하면 원하는 모든 작업을 수행 할 수있는 모든 Range 개체 필드에 액세스 할 수 있습니다. –

관련 문제