Revert objects on site instead of admin using django simple history

我的未来我决定 提交于 2020-02-06 12:50:00

问题


I have employed django simple history package on the admin site to be able to track and revert to previous versions of the object of the model. I am designing a web form that allows users to change instances of the model object using model form on django and would like to allow the users to view and revert to previous versions. Also to allow them to see what are the changes compared to the current version.

With the code below I am able to get the list of historical records on my template under histoire.

class CompanyDetailView(LoginRequiredMixin,generic.DetailView):
    model = Company

    def get_context_data(self, **kwargs):
         context = super(CompanyDetailView, self).get_context_data(**kwargs)
         company_instance = self.object
         context['histoire'] = company_instance.history.all()
         return context

In my template,

<p>
    Previous versions:
    {% for item in histoire %}
      <li>
        {{ item }} submitted by {{ item.history_user }}  {{
         item.history_object }}
      </li>
      {% endfor %}

</p>

But ideally I want item.history_object to be a link that users can view the previous object and be able to revert if desired.


回答1:


I did something similar by adding HistoricForm to my model forms.

class MyModelForm(HistoricForm, ModelForm):
     ...

HistoricForm takes and extra history_id kwarg. If history_id is provided HistoricForm swaps the ModelForm instance with the historic_instance (what your instance looked like at the time of history_id). This way your form will show the historic version of your object.

class HistoricForm(object):
    def __init__(self, *args, **kwargs):
        self.history_id = kwargs.pop('history_id', None)
        instance = kwargs.get('instance')
        if instance and self.history_id:
            kwargs['instance'] = self.get_historic_instance(instance)

        super(HistoricForm, self).__init__(*args, **kwargs)

    def get_historic_instance(self, instance):
        model = self._meta.model
        queryset = getattr(model, 'history').model.objects
        historic_instance = queryset.get(**{
            model._meta.pk.attname: instance.pk,
            'history_id': self.history_id,
        }).instance
        return historic_instance

If history_id is not provided the ModelForm works as usual.

You can revert by showing the historic instance and save (this way you will post your historic data).



来源:https://stackoverflow.com/questions/47465027/revert-objects-on-site-instead-of-admin-using-django-simple-history

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!