def change_view(self, request, object_id, form_url='', extra_context=None): if request.method == 'POST': self.bAllowAddLink = False else: self.bAllowAddLink = True if self.form_links != []: initial = self.get_linkform_formset_initial(request,object_id) link_admin_obj_formsets = [] prefixes = {} for LinkInitial, LinkForm, LinkObjFormSet, link_m2m, link_initsearch in zip(initial, self.get_linkform_forms(request), self.get_linkform_formsets(request), self.get_linkform_m2ms(request), self.get_linkform_initsearchs(request)): #prefix = LinkObjFormSet.get_default_prefix() prefix = LinkForm.Meta.class_name + "_set" prefixes[prefix] = prefixes.get(prefix, 0) + 1 if prefixes[prefix] != 1 or not prefix: prefix = "%s-%s" % (prefix, prefixes[prefix]) if request.method == "POST": formset = LinkObjFormSet(request.POST, request.FILES, prefix = prefix) if formset.is_valid(): formset = self.sort_linkform_formset(request, formset) link_admin_obj_formset = LinkFormAdminFormset(formset = formset) else: link_admin_obj_formset = LinkFormAdminFormset(formset = LinkObjFormSet(prefix = prefix)) else: link_obj_formset = LinkObjFormSet(initial = LinkInitial, prefix = prefix) link_admin_obj_formset = LinkFormAdminFormset(formset = link_obj_formset) link_admin_obj_formset.link_m2m = link_m2m link_admin_obj_formset.link_init_search = link_initsearch link_admin_obj_formsets.append(link_admin_obj_formset) extra_context_cur = { 'link_form_admin_formsets':link_admin_obj_formsets, 'bAllowAddLink': self.bAllowAddLink, } if extra_context == None: extra_context = {} extra_context.update(extra_context_cur) # link admin ########################################################## if self.modelform_links != []: model = self.model opts = model._meta obj = self.get_object(request, unquote(object_id)) if request.method == 'POST' and "_saveasnew" in request.POST: return self.add_view(request, form_url=reverse('admin:%s_%s_add' % (opts.app_label, opts.module_name), current_app=self.admin_site.name)) ModelForm = self.get_form(request, obj) formsets = [] link_instances = self.get_link_instances(request, obj) if request.method == 'POST': form = ModelForm(request.POST, request.FILES, instance=obj) if form.is_valid(): form_validated = True new_object = self.save_form(request, form, change=True) else: form_validated = False new_object = obj prefixes = {} for FormSet, link in zip(self.get_link_formsets(request, new_object), link_instances): prefix = FormSet.get_default_prefix() prefixes[prefix] = prefixes.get(prefix, 0) + 1 if prefixes[prefix] != 1 or not prefix: prefix = "%s-%s" % (prefix, prefixes[prefix]) formset = FormSet(request.POST, request.FILES, instance=new_object, prefix=prefix, queryset=link.queryset(request)) formsets.append(formset) if form_validated: pass else: prefixes = {} for FormSet, link in zip(self.get_link_formsets(request, obj), link_instances): prefix = FormSet.get_default_prefix() prefixes[prefix] = prefixes.get(prefix, 0) + 1 if prefixes[prefix] != 1 or not prefix: prefix = "%s-%s" % (prefix, prefixes[prefix]) formset = FormSet(instance=obj, prefix=prefix, queryset=link.queryset(request)) formsets.append(formset) link_admin_formsets = [] for link, formset in zip(link_instances, formsets): fieldsets = list(link.get_fieldsets(request, obj)) readonly = list(link.get_readonly_fields(request, obj)) prepopulated = dict(link.get_prepopulated_fields(request, obj)) link_admin_formset = LinkModelAdminFormSet(link, formset, fieldsets, prepopulated, readonly, model_admin=self) link_admin_formset.link_m2m = link.link_m2m link_admin_formset.obj_id = object_id link_admin_formsets.append(link_admin_formset) context_tmp = { 'link_admin_formsets': link_admin_formsets, } if extra_context == None: extra_context = {} extra_context.update(context_tmp or {}) return super(MyModelAdmin, self).change_view(request,object_id, form_url,extra_context)
def add_view(self, request, form_url='', extra_context=None): if not self.has_add_permission(request): raise PermissionDenied # LineInlineFormAdimin link_admin_obj_formset = None link_admin_obj_formsets = [] self.bAllowAddLink = False if self.form_links != []: prefixes = {} for LinkForm, LinkMany2Many, LinkInitSearch, LinkObjFormSet in zip(self.get_linkform_forms(request), self.get_linkform_m2ms(request), self.get_linkform_initsearchs(request), self.get_linkform_formsets(request)): #prefix = LinkObjFormSet.get_default_prefix() prefix = LinkForm.Meta.class_name + "_set" prefixes[prefix] = prefixes.get(prefix, 0) + 1 if prefixes[prefix] != 1 or not prefix: prefix = "%s-%s" % (prefix, prefixes[prefix]) if request.method == "POST": formset = LinkObjFormSet(request.POST, request.FILES, prefix = prefix) if formset.is_valid(): formset = self.sort_linkform_formset(request, formset, True) link_admin_obj_formset = LinkFormAdminFormset(formset = formset) else: link_admin_obj_formset = LinkFormAdminFormset(formset = LinkObjFormSet(prefix = prefix)) else: link_admin_obj_formset = LinkFormAdminFormset(formset = LinkObjFormSet(prefix = prefix)) link_admin_obj_formset.link_m2m = LinkMany2Many link_admin_obj_formset.link_init_search = LinkInitSearch link_admin_obj_formsets.append(link_admin_obj_formset) extra_context_cur = { 'link_form_admin_formsets':link_admin_obj_formsets, 'bAllowAddLink': self.bAllowAddLink, } if extra_context == None: extra_context = {} extra_context.update(extra_context_cur) if self.modelform_links != []: # LineInlineModelAdimin #model = self.model #opts = model._meta formsets = [] link_instances = self.get_link_instances(request, None) if request.method == 'POST': new_object = self.model() prefixes = {} for FormSet, link in zip(self.get_link_formsets(request), link_instances): prefix = FormSet.get_default_prefix() prefixes[prefix] = prefixes.get(prefix, 0) + 1 if prefixes[prefix] != 1 or not prefix: prefix = "%s-%s" % (prefix, prefixes[prefix]) formset = FormSet(data=request.POST, files=request.FILES, instance=new_object, save_as_new="_saveasnew" in request.POST, prefix=prefix, queryset=link.queryset(request)) formsets.append(formset) else: prefixes = {} for FormSet, link in zip(self.get_link_formsets(request), link_instances): prefix = FormSet.get_default_prefix() prefixes[prefix] = prefixes.get(prefix, 0) + 1 if prefixes[prefix] != 1 or not prefix: prefix = "%s-%s" % (prefix, prefixes[prefix]) formset = FormSet(instance=self.model(), prefix=prefix, queryset=link.queryset(request)) formsets.append(formset) link_admin_formsets = [] link_media = self.media #should implement it for further dev !!!! for link, formset in zip(link_instances, formsets): fieldsets = list(link.get_fieldsets(request)) readonly = list(link.get_readonly_fields(request)) prepopulated = dict(link.get_prepopulated_fields(request)) link_admin_formset = LinkModelAdminFormSet(link, formset, fieldsets, prepopulated, readonly, model_admin=self) link_admin_formset.link_m2m = link.link_m2m link_admin_formsets.append(link_admin_formset) context_tmp = { 'link_admin_formsets': link_admin_formsets, 'link_media':link_media, } if extra_context == None: extra_context = {} extra_context.update(context_tmp or {}) extra_auto = { 'is_add_new':'True', 'CreateTime': time.strftime("%H:%M:%S") , 'CreateDate':time.strftime("%Y-%m-%d") } if extra_context == None: extra_context = {} extra_context.update(extra_auto or {}) return super(MyModelAdmin, self).add_view(request,form_url,extra_context)