Django 1.6 到 1.11 Admin.py 迁移
Posted
技术标签:
【中文标题】Django 1.6 到 1.11 Admin.py 迁移【英文标题】:Django 1.6 to 1.11 Admin.py migration 【发布时间】:2018-04-13 16:46:37 【问题描述】:我正在尝试将旧的 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)
【问题讨论】:
请不要把代码扔给我们检查。如果您有多个问题,请将您的代码分成几个问题,并告诉我们您尝试了什么、确切的错误消息是什么、抛出它的行等。 立即升级 5 个主要版本永远不会奏效,您应该逐步升级。 无论如何,您需要告诉我们您遇到的实际问题以及遇到的错误。 第一个在 1.6 中有效而在 1.11 中无效的东西就在顶部。方法 def queryset(self, request): in class FilteredUserAdmin(admin.ModelAdmin): 在 1.11 中没有被调用。 【参考方案1】:结果很简单。在我上面的代码中,有一些方法,比如...
def 查询集(自我,请求):
... 这在 Django 1.6 中很好。但是 Django 1.11 的正确覆盖应该是...
def get_queryset(self, request):
【讨论】:
以上是关于Django 1.6 到 1.11 Admin.py 迁移的主要内容,如果未能解决你的问题,请参考以下文章
升级到 Django 1.11 后 append_slash 不再起作用
从 Django 1.11 迁移到 2.1:“fieldsets[2][1]”中有重复的字段
如何在 Django 1.11 中运行 celery worker