Skip to content

Instantly share code, notes, and snippets.

@elcolie
Last active October 2, 2017 11:10
Show Gist options
  • Save elcolie/081017adc7b5e2fd7a3a38b0573597c6 to your computer and use it in GitHub Desktop.
Save elcolie/081017adc7b5e2fd7a3a38b0573597c6 to your computer and use it in GitHub Desktop.
from django.contrib import admin
from django.contrib.auth.models import User
from import_export import resources, fields, widgets
from import_export.admin import ImportExportMixin
from model_controller.admins import ModelControllerAdmin
from reversion.admin import VersionAdmin
from soken_web.apps.zipcodes.models import ZIPCodeAddress, ZIPCodeAddressImportExport
ZIPCODE_BASIC_FIELDS = [
'zipcode',
'region',
'prefecture',
'city',
'town',
'building',
]
IMPORT_COLUMNS = ZIPCODE_BASIC_FIELDS + ['created_user', 'updated_user']
class ZIPCodeAddressAdmin(VersionAdmin, ModelControllerAdmin):
list_display = ZIPCODE_BASIC_FIELDS
search_fields = ZIPCODE_BASIC_FIELDS
class ZIPCodeAddressResource(resources.ModelResource):
zipcode = fields.Field(widget=widgets.CharWidget())
created_user = fields.Field(column_name='created_user', attribute='created_user', widget=widgets.ForeignKeyWidget(User, 'username'))
updated_user = fields.Field(column_name='created_user', attribute='created_user', widget=widgets.ForeignKeyWidget(User, 'username'))
class Meta:
model = ZIPCodeAddressImportExport
exclude = ['id', ] # Effect on Import
fields = IMPORT_COLUMNS
skip_unchanged = True
report_skipped = True
import_id_fields = ['zipcode', ]
export_order = IMPORT_COLUMNS
class ZIPCodeADdressImportExportAdmin(ImportExportMixin, ModelControllerAdmin):
resource_class = ZIPCodeAddressResource
list_display = ZIPCODE_BASIC_FIELDS
search_fields = ZIPCODE_BASIC_FIELDS
admin.site.register(ZIPCodeAddress, ZIPCodeAddressAdmin)
admin.site.register(ZIPCodeAddressImportExport, ZIPCodeADdressImportExportAdmin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment