如何将 django-reversion 添加到使用 django 和 django-rest 框架开发的应用程序
How to add django-reversion to an app developed using django and django-rest framework
我有一个使用 Django 和 Django Rest 框架开发的应用程序。我想将 django-reversion 功能添加到我的应用程序中。
我已经尝试 http://django-reversion.readthedocs.org/en/latest/api.html#low-level-api 但我未能对我的应用程序进行具体更改。
下面是应用程序的模块,我想在其中包含 Django 还原以在对象被删除时恢复它们。如何为以下模块设置 django-reversion 配置
admin.py:-
from django.contrib import admin
from.models import Category
admin.site.register(Category)
models.py:-
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=64, unique=True)
def __unicode__(self):
return self.name
serializers.py:-
from rest_framework import serializers
from .models import Category
class CategorySerializer(serializers.ModelSerializer):
courses = serializers.HyperlinkedRelatedField(
many=True
read_only=True
view_name='course-detail'
)
class Meta:
model = Category
fields = ('pk', 'name', 'courses',)
urls.py :-
from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
from .import views
from django.conf.urls import include
category_list = views.CategoryViewSet.as_view({
'get': 'list',
'post': 'create'
})
category_detail = views.CategoryViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy',
})
urlpatterns = format_suffix_patterns([
url(r'^categories/$',
category_list,
name='category-list'),
url(r'^categories/(?P<pk>[0-9]+)/$',
category_detail,
name='category-detail'),
])
urlpatterns += [
url(r'^api-auth/', include('rest_framework.urls',
namespace='rest_framework')),
]
views.py :-
from rest_framework import permissions
from rest_framework import viewsets
from .models import Category
from .serializers import CategorySerializer
class CategoryViewSet(viewsets.ModelViewSet):
queryset = Category.objects.all()
serializer_class = CategorySerializer
permission_classes = (permissions.IsAuthenticatedorReadOnly,)
The simplest way to create revisions is to use reversion.middleware.RevisionMiddleware. This will automatically wrap every request in a revision, ensuring that all changes to your models will be added to their version history.
To enable the revision middleware, simply add it to your MIDDLEWARE_CLASSES
setting as follows:
MIDDLEWARE_CLASSES = (
'reversion.middleware.RevisionMiddleware',
# Other middleware goes here...
)
任何更复杂的事情都需要通过您的代码添加 API 调用,以 您 决定的方式包装特定的 save
调用。
admin.py
from django.contrib import admin
from.models import Category
import reversion
class BaseReversionAdmin(reversion.VersionAdmin):
pass
admin.site.register(Category, BaseReversionAdmin)
还添加了对 installed_apps 和中间件类的还原。
我终于看到了 "recover deleted objects button"。
我发现,由于 rest_framework.viewsets.ModelViewSet
继承自 django.views.generic.View
,因此您还可以使用 reversion.views.RevisionMixin
创建修订版,而不必根据需要使用中间件。
从上面的问题来看,这看起来像下面这样:
from rest_framework import permissions
from rest_framework import viewsets
from .models import Category
from .serializers import CategorySerializer
from reversion.views import RevisionMixin
class CategoryViewSet(RevisionMixin, viewsets.ModelViewSet):
queryset = Category.objects.all()
serializer_class = CategorySerializer
permission_classes = (permissions.IsAuthenticatedorReadOnly,)
您可以在此处详细了解如何使用 RevisionMixin
:https://django-reversion.readthedocs.io/en/stable/views.html#reversion-views-revisionmixin
我有一个使用 Django 和 Django Rest 框架开发的应用程序。我想将 django-reversion 功能添加到我的应用程序中。
我已经尝试 http://django-reversion.readthedocs.org/en/latest/api.html#low-level-api 但我未能对我的应用程序进行具体更改。
下面是应用程序的模块,我想在其中包含 Django 还原以在对象被删除时恢复它们。如何为以下模块设置 django-reversion 配置
admin.py:-
from django.contrib import admin
from.models import Category
admin.site.register(Category)
models.py:-
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=64, unique=True)
def __unicode__(self):
return self.name
serializers.py:-
from rest_framework import serializers
from .models import Category
class CategorySerializer(serializers.ModelSerializer):
courses = serializers.HyperlinkedRelatedField(
many=True
read_only=True
view_name='course-detail'
)
class Meta:
model = Category
fields = ('pk', 'name', 'courses',)
urls.py :-
from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
from .import views
from django.conf.urls import include
category_list = views.CategoryViewSet.as_view({
'get': 'list',
'post': 'create'
})
category_detail = views.CategoryViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy',
})
urlpatterns = format_suffix_patterns([
url(r'^categories/$',
category_list,
name='category-list'),
url(r'^categories/(?P<pk>[0-9]+)/$',
category_detail,
name='category-detail'),
])
urlpatterns += [
url(r'^api-auth/', include('rest_framework.urls',
namespace='rest_framework')),
]
views.py :-
from rest_framework import permissions
from rest_framework import viewsets
from .models import Category
from .serializers import CategorySerializer
class CategoryViewSet(viewsets.ModelViewSet):
queryset = Category.objects.all()
serializer_class = CategorySerializer
permission_classes = (permissions.IsAuthenticatedorReadOnly,)
The simplest way to create revisions is to use reversion.middleware.RevisionMiddleware. This will automatically wrap every request in a revision, ensuring that all changes to your models will be added to their version history.
To enable the revision middleware, simply add it to your
MIDDLEWARE_CLASSES
setting as follows:
MIDDLEWARE_CLASSES = (
'reversion.middleware.RevisionMiddleware',
# Other middleware goes here...
)
任何更复杂的事情都需要通过您的代码添加 API 调用,以 您 决定的方式包装特定的 save
调用。
admin.py
from django.contrib import admin
from.models import Category
import reversion
class BaseReversionAdmin(reversion.VersionAdmin):
pass
admin.site.register(Category, BaseReversionAdmin)
还添加了对 installed_apps 和中间件类的还原。
我终于看到了 "recover deleted objects button"。
我发现,由于 rest_framework.viewsets.ModelViewSet
继承自 django.views.generic.View
,因此您还可以使用 reversion.views.RevisionMixin
创建修订版,而不必根据需要使用中间件。
从上面的问题来看,这看起来像下面这样:
from rest_framework import permissions
from rest_framework import viewsets
from .models import Category
from .serializers import CategorySerializer
from reversion.views import RevisionMixin
class CategoryViewSet(RevisionMixin, viewsets.ModelViewSet):
queryset = Category.objects.all()
serializer_class = CategorySerializer
permission_classes = (permissions.IsAuthenticatedorReadOnly,)
您可以在此处详细了解如何使用 RevisionMixin
:https://django-reversion.readthedocs.io/en/stable/views.html#reversion-views-revisionmixin