1 Star 0 Fork 0

武松 / django-import-export

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
admin.py 1.55 KB
Copy Edit Web IDE Raw Blame History
from django.contrib import admin
from import_export.admin import ExportActionModelAdmin, ImportExportMixin, ImportMixin
from import_export.resources import ModelResource
from .forms import CustomConfirmImportForm, CustomImportForm
from .models import Author, Book, Category, Child, EBook
class ChildAdmin(ImportMixin, admin.ModelAdmin):
pass
class BookResource(ModelResource):
class Meta:
model = Book
def for_delete(self, row, instance):
return self.fields['name'].clean(row) == ''
class BookAdmin(ImportExportMixin, admin.ModelAdmin):
list_display = ('name', 'author', 'added')
list_filter = ['categories', 'author']
resource_class = BookResource
class CategoryAdmin(ExportActionModelAdmin):
pass
class AuthorAdmin(ImportMixin, admin.ModelAdmin):
pass
class CustomBookAdmin(BookAdmin):
"""BookAdmin with custom import forms"""
def get_import_form(self):
return CustomImportForm
def get_confirm_import_form(self):
return CustomConfirmImportForm
def get_form_kwargs(self, form, *args, **kwargs):
# update kwargs with authors (from CustomImportForm.cleaned_data)
if isinstance(form, CustomImportForm):
if form.is_valid():
author = form.cleaned_data['author']
kwargs.update({'author': author.id})
return kwargs
admin.site.register(Book, BookAdmin)
admin.site.register(Category, CategoryAdmin)
admin.site.register(Author, AuthorAdmin)
admin.site.register(Child, ChildAdmin)
admin.site.register(EBook, CustomBookAdmin)

Comment ( 0 )

Sign in for post a comment

1
https://gitee.com/tompeppa/django-import-export.git
git@gitee.com:tompeppa/django-import-export.git
tompeppa
django-import-export
django-import-export
master

Search

102255 3a0e046c 1850385 102255 7aaa926c 1850385