2016-10-04 5 views
0

내 프로젝트에서 LogEntry를 저장하기 위해 Django simple history을 사용하고 있습니다. 나는 Django rest framework (DRF)과 Angularjs를 사용하는 프론트 엔드를 사용하여 API를 빌드했습니다. 개체에 대한 LogEntry 기록은 아래 그림과 같이 문제없이 저장됩니다!admin에서 Django의 간단한 기록 로그에 액세스 할 수 있습니까?

enter image description here

나는 아무 문제없이 장고 관리자에서 객체 기록에 액세스 할 수

from datetime import datetime 
from django.db import models 
from simple_history.models import HistoricalRecords 


class Person(models.Model): 

    """ Person model""" 

    first_name = models.CharField(max_length=255) 
    last_name = models.CharField(max_length=255) 
    workphone = models.CharField(max_length=15, blank=True, default='') 
    avatar = models.FileField(upload_to="", blank=True, default='') 
    createdtstamp = models.DateTimeField(auto_now_add=True) 
    history = HistoricalRecords(inherit=True) 


    def __unicode__(self): 
     return "%s" % self.first_name 

models.py. 하지만, 어떻게 장고 관리자 외부에서 LogEntry 기록에 액세스 할 수 있습니까? 나는 로그 쿼리 세트를 직렬화하고 json 포맷으로 응답을 리턴하고 싶다.

내가 아는 것과 지금까지 한 것은 무엇입니까?

from person.models import Person 
from datetime import datetime 

>>> person = Person.objects.all() 
>>> person.history.all() 
+0

이 질문은 ATM의 질문과 같습니다. 제발 최선을 다해 노력하십시오 – e4c5

+0

나는 @ e4c5에 최선을 다했습니다! – MysticCodes

답변

0

당신이있는 ListView를 사용하여 데이터를 검색 할 수 있습니다 ...이처럼 뷰에서 다음

class HistorySerializer(serializers.ModelSerializer): 
    class Meta: 
     model=Person 

및 ...

class HistoryViewset(viewsets.ModelViewSet): 
    queryset = Person.objects.all() 
    serializer_class = HistorySerializer 

    @list_route(methods=["GET", "POST"]) 
    def history(self,request): 
     var=Person.history.all() 
     serialized_data=HistorySerializer(var,many=True) 
     return Response(serialized_data.data) 
0

을 자신의 historyserializer을합니다.

모델 models.py

from django.db import models 
from simple_history.models import HistoricalRecords 

# Create your models here. 

class Poll(models.Model): 
    question = models.CharField(max_length=200) 
    history = HistoricalRecords() 

보기 views.py

from django.shortcuts import render 
from django.views.generic import ListView 
from .models import Poll 

# Create your views here. 

class PollHistoryView(ListView): 
    template_name = "pool_history_list.html" 
    def get_queryset(self): 
     history = Poll.history.all() 
     return history 
:

아래의 절차를 따르십시오 장고 - 간단한 역사를 올바르게 구성되어 가정

템플릿 pool_history_list.html

<table> 
    <thead> 
     <tr> 
      <th>Question</th> 
      <th>History Date/Time</th> 
      <th>History Action</th> 
      <th>History User</th> 
     </tr> 
    </thead> 
    <tbody> 
     {% for t in object_list %} 
     <tr> 
      <td>{{ t.id }}</td> 
      <td>{{ t.question }}</td> 
      <td>{{ t.history_date }}</td> 
      <td>{{ t.get_history_type_display }}</td> 
      <td>{{ t.history_user }}</td> 
     </tr> 
     {% endfor %} 
    </tbody> 
</table> 
관련 문제