我正在尝试将旧的Django 1.6 admin.py代码移植到Django 1.11中,无论出于什么原因,它都无法在1.11上运行。我发送给你下面的代码,所以你们中的一些人可能会给我一些旧代码的区域,这些区域可能不适用于Django 1.11。提前感谢任何暗示。
---- admin.py -----
from django.contrib import admin
from webpad.models import Layout, Element, LayoutElement, Session, Click, DataGroup, DataElement, ReportTemplate, ReportBlock, Stream, Participant, Observer, Additional
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django import forms
class FilteredUserAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.user = request.user
obj.save()
def queryset(self, request):
qs = super(FilteredUserAdmin, self).queryset(request)
return qs.filter(user=request.user)
class ElementAdmin(FilteredUserAdmin):
fieldsets = [
(None, {'fields': ['name','group','connotation','color','add_to_video']}),
]
class AdditionalInline(admin.StackedInline):
model = Additional
can_delete = False
class UserAdmin(UserAdmin):
inlines = (AdditionalInline, )
class ParticipantInline(admin.TabularInline):
model = Participant
extra = 0
class StreamAdmin(FilteredUserAdmin):
fieldsets = [
(None, {'fields': ['stream_name','active']}),
]
inlines = [ParticipantInline]
class LayoutElementInlineForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(LayoutElementInlineForm, self).__init__(*args, **kwargs)
self.fields['element'].queryset = Element.objects.filter(user=self.request.user)
class LayoutElementInline(admin.StackedInline):
model = LayoutElement
extra = 0
form = LayoutElementInlineForm
def queryset(self, request):
self.form.request = request
class LayoutAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(LayoutAdminForm, self).__init__(*args, **kwargs)
class LayoutAdmin(FilteredUserAdmin):
fieldsets = [
(None, {'fields': ['layout_description','columns']}),
]
inlines = [LayoutElementInline]
form = LayoutAdminForm
def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)
for obj in formset.deleted_objects:
obj.delete()
for instance in instances:
instance.user = request.user
instance.save()
formset.save_m2m()
def get_form(self, request, obj=None, **kwargs):
ModelForm = super(LayoutAdmin, self).get_form(request, obj, **kwargs)
class ModelFormMetaClass(ModelForm):
def __new__(cls, *args, **kwargs):
kwargs['request'] = request
return ModelForm(*args, **kwargs)
return ModelFormMetaClass
class ObserverInlineForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ObserverInlineForm, self).__init__(*args, **kwargs)
self.fields['layout'].queryset = Layout.objects.filter(user=self.request.user)
stream_list = Stream.objects.filter(user=self.request.user, active=True)
self.fields['participant'].queryset = Participant.objects.filter(stream__in = stream_list)
class ObserverInline(admin.TabularInline):
model = Observer
extra = 0
form = ObserverInlineForm
def queryset(self, request):
self.form.request = request
class ClickInline(admin.TabularInline):
fieldsets = [
(None, {'fields': ['element','observer','timestamp']}),
]
model = Click
extra = 0
class SessionAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(SessionAdminForm, self).__init__(*args, **kwargs)
stream_list = Stream.objects.filter(user=self.request.user, active=True)
self.fields['participant'].queryset = Participant.objects.filter(stream__in = stream_list)
class SessionAdmin(FilteredUserAdmin):
fieldsets = [
(None, {'fields': ['participant']}),
('Date information', {'fields': ['session_start','session_finish'], 'classes': ['collapse']}),
]
form = SessionAdminForm
inlines = [ObserverInline, ClickInline]
def get_form(self, request, obj=None, **kwargs):
ModelForm = super(SessionAdmin, self).get_form(request, obj, **kwargs)
class ModelFormMetaClass(ModelForm):
def __new__(cls, *args, **kwargs):
kwargs['request'] = request
return ModelForm(*args, **kwargs)
return ModelFormMetaClass
class DataElementInlineForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(DataElementInlineForm, self).__init__(*args, **kwargs)
self.fields['element'].queryset = Element.objects.filter(user=self.request.user)
class DataElementInline(admin.StackedInline):
model = DataElement
extra = 0
form = DataElementInlineForm
def queryset(self, request):
self.form.request = request
class DataGroupAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(DataGroupAdminForm, self).__init__(*args, **kwargs)
class DataGroupAdmin(FilteredUserAdmin):
fieldsets = [
(None, {'fields': ['visual_representation','group_description']}),
]
inlines = [DataElementInline]
form = DataGroupAdminForm
def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)
for obj in formset.deleted_objects:
obj.delete()
for instance in instances:
instance.user = request.user
instance.save()
formset.save_m2m()
def get_form(self, request, obj=None, **kwargs):
ModelForm = super(DataGroupAdmin, self).get_form(request, obj, **kwargs)
class ModelFormMetaClass(ModelForm):
def __new__(cls, *args, **kwargs):
kwargs['request'] = request
return ModelForm(*args, **kwargs)
return ModelFormMetaClass
class ReportBlockInlineForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ReportBlockInlineForm, self).__init__(*args, **kwargs)
self.fields['data_group'].queryset = DataGroup.objects.filter(user=self.request.user)
class ReportBlockInline(admin.TabularInline):
model = ReportBlock
extra = 0
form = ReportBlockInlineForm
def queryset(self, request):
self.form.request = request
class ReportTemplateAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(ReportTemplateAdminForm, self).__init__(*args, **kwargs)
class ReportTemplateAdmin(FilteredUserAdmin):
fieldsets = [
(None, {'fields': ['report_description']}),
]
inlines = [ReportBlockInline]
form = ReportTemplateAdminForm
def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)
for obj in formset.deleted_objects:
obj.delete()
for instance in instances:
instance.user = request.user
instance.save()
formset.save_m2m()
def get_form(self, request, obj=None, **kwargs):
ModelForm = super(ReportTemplateAdmin, self).get_form(request, obj, **kwargs)
class ModelFormMetaClass(ModelForm):
def __new__(cls, *args, **kwargs):
kwargs['request'] = request
return ModelForm(*args, **kwargs)
return ModelFormMetaClass
admin.site.site_header = "Feedback.Studio"
admin.site.site_title = "Feedback.Studio"
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
admin.site.register(Element, ElementAdmin)
admin.site.register(Session, SessionAdmin)
admin.site.register(Stream, StreamAdmin)
admin.site.register(Layout, LayoutAdmin)
admin.site.register(DataGroup, DataGroupAdmin)
admin.site.register(ReportTemplate, ReportTemplateAdmin)
答案 0 :(得分:1)
事实证明这很简单。在我上面的代码中,有一些方法,比如......
def queryset(self,request):
......这在Django 1.6中很好。但是Django 1.11的正确覆盖应该是......
def get_queryset(self,request):