내 웹 애플리케이션의 프론트 엔드에서 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와 같을 것입니다.
어떤 도움을 주셔서 감사합니다.
를 해결하기 위해 범위는 객체 : JSON을 deserialize 할 필요가 있으므로 사용자 정의 필드도 필요합니다. 범위 배열의 각 부분에 대해 하나입니까? 다른 범위 필드에서 언급 된 n? – allyjweir
나는 당신이 원하는 필드를 가지고 직렬화 (Annotation-> Range 객체 json)를 처리 할 수있는 serializer를 생성하는 Range 객체 (보통 Python 클래스, Django 모델이 아닌)를 정의하는 것이 더 좋을 것이라고 생각한다. deserialization (Range 객체 json -> Range 객체) 및 원하는 모든 유효성 검사. 이것을 Annotation 모델 시리얼 라이저 (중첩 시리얼 라이저)의 필드로 포함하십시오. 그런 다음 Annotation serializer의 save() 메서드를 재정의하면 원하는 모든 작업을 수행 할 수있는 모든 Range 개체 필드에 액세스 할 수 있습니다. –