2016-10-04 11 views
0

LogEntryを保存するプロジェクトにはDjango simple historyを使用しています。私はAPIをDjangoの残りのフレームワーク(DRF)とフロントエンドを使用してAngularjsを使用してビルドしています。 オブジェクトのLogEntry履歴は、以下に示すように問題なく保存されます!adminからDjangoの単純履歴ログにアクセスできますか?

enter image description here

models.py

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 

私は何の問題もなく、Djangoの管理からオブジェクト履歴にアクセスすることができます。しかし、 Djangoの管理者以外の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

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

リストビューを使用してデータを取得することができます...あなたの意見で

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

してから...このような独自の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> 
関連する問題